You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0100-0199/0180.Consecutive Numbers/README_EN.md
+44-8
Original file line number
Diff line number
Diff line change
@@ -53,26 +53,62 @@ Logs table:
53
53
54
54
## Solutions
55
55
56
+
**Solution 1: Two Joins**
57
+
58
+
We can use two joins to solve this problem.
59
+
60
+
First, we perform a self-join with the condition `l1.num = l2.num` and `l1.id = l2.id - 1`, so that we can find all numbers that appear at least twice in a row. Then, we perform another self-join with the condition `l2.num = l3.num` and `l2.id = l3.id - 1`, so that we can find all numbers that appear at least three times in a row. Finally, we only need to select the distinct `l2.num`.
61
+
62
+
**Solution 2: Window Function**
63
+
64
+
We can use the window functions `LAG` and `LEAD` to obtain the `num` of the previous row and the next row of the current row, and record them in the fields $a$ and $b$, respectively. Finally, we only need to filter out the rows where $a = num$ and $b = num$, which are the numbers that appear at least three times in a row. Note that we need to use the `DISTINCT` keyword to remove duplicates from the results.
65
+
66
+
We can also group the numbers by using the `IF` function to determine whether the `num` of the current row is equal to the `num` of the previous row. If they are equal, we set it to $0$, otherwise we set it to $1$. Then, we use the window function `SUM` to calculate the prefix sum, which is the grouping identifier. Finally, we only need to group by the grouping identifier and filter out the numbers with a row count greater than or equal to $3$ in each group. Similarly, we need to use the `DISTINCT` keyword to remove duplicates from the results.
67
+
56
68
<!-- tabs:start -->
57
69
58
70
### **SQL**
59
71
72
+
```sql
73
+
# Write your MySQL query statement below
74
+
SELECT DISTINCTl2.numAS ConsecutiveNums
75
+
FROM
76
+
Logs AS l1
77
+
JOIN Logs AS l2 ONl1.id=l2.id-1ANDl1.num=l2.num
78
+
JOIN Logs AS l3 ONl2.id=l3.id-1ANDl2.num=l3.num;
79
+
```
80
+
81
+
```sql
82
+
# Write your MySQL query statement below
83
+
WITH
84
+
T AS (
85
+
SELECT
86
+
*,
87
+
LAG(num) OVER () AS a,
88
+
LEAD(num) OVER () AS b
89
+
FROM Logs
90
+
)
91
+
SELECT DISTINCT num AS ConsecutiveNums
92
+
FROM T
93
+
WHERE a = num AND b = num;
94
+
```
95
+
60
96
```sql
61
97
# Write your MySQL query statement below
62
98
WITH
63
-
tAS (
99
+
TAS (
64
100
SELECT
65
101
*,
66
-
CASE
67
-
WHEN (LAG(num) OVER (ORDER BY id)) = num THEN 0
68
-
ELSE 1
69
-
END AS mark
102
+
IF(num = (LAG(num) OVER ()), 0, 1) AS st
70
103
FROM Logs
71
104
),
72
-
p AS (SELECT num, SUM(mark) OVER (ORDER BY id) AS gid FROM t)
Copy file name to clipboardexpand all lines: solution/0600-0699/0610.Triangle Judgement/README_EN.md
+4
Original file line number
Diff line number
Diff line change
@@ -49,6 +49,10 @@ Triangle table:
49
49
50
50
## Solutions
51
51
52
+
**Solution 1: IF Statement + Triangle Inequality**
53
+
54
+
The condition for whether three sides can form a triangle is that the sum of any two sides is greater than the third side. Therefore, we can use an `IF` statement to determine whether this condition is satisfied. If it is satisfied, we return `Yes`, otherwise we return `No`.
Copy file name to clipboardexpand all lines: solution/1100-1199/1164.Product Price at a Given Date/README_EN.md
+18-23
Original file line number
Diff line number
Diff line change
@@ -53,38 +53,33 @@ Products table:
53
53
54
54
## Solutions
55
55
56
+
**Solution 1: Subquery + Join**
57
+
58
+
We can use a subquery to find the price of the last price change for each product before the given date, and record it in the `P` table. Then, we can find all `product_id`s in the `T` table. Finally, we can left join the `T` table with the `P` table on `product_id` to get the final result.
59
+
56
60
<!-- tabs:start -->
57
61
58
62
### **SQL**
59
63
60
64
```sql
61
65
# Write your MySQL query statement below
62
-
SELECT
63
-
p1.product_idAS product_id,
64
-
IFNULL(p2.price, 10) AS price
65
-
FROM
66
-
(
67
-
SELECT DISTINCT
68
-
(product_id) AS product_id
66
+
WITH
67
+
T AS (SELECT DISTINCT product_id FROM Products),
68
+
P AS (
69
+
SELECT product_id, new_price AS price
69
70
FROM Products
70
-
) AS p1
71
-
LEFT JOIN (
72
-
SELECT
73
-
t1.product_id,
74
-
t1.new_priceAS price
75
-
FROM
76
-
Products AS t1
77
-
JOIN (
78
-
SELECT
79
-
product_id,
80
-
MAX(change_date) AS change_date
71
+
WHERE
72
+
(product_id, change_date) IN (
73
+
SELECT product_id, MAX(change_date) AS change_date
0 commit comments