Skip to content

Commit 168ef90

Browse files
authored
feat: update solutions to lc problems (#1790)
* No.0603.Consecutive Available Seats * No.0613.Shortest Distance in a Line * No.1501.Countries You Can Safely Invest In * No.1795.Rearrange Products Table
1 parent 35ee416 commit 168ef90

File tree

12 files changed

+168
-172
lines changed

12 files changed

+168
-172
lines changed

solution/0600-0699/0603.Consecutive Available Seats/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ Cinema 表:
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:自连接**
61+
62+
我们可以使用自连接的方式,将相邻的两个座位连接起来,然后筛选出连续空余的座位并去重排序即可。
63+
64+
**方法二:窗口函数**
65+
66+
我们也可以使用 `LAG``LEAD` 函数(或者 `SUM() OVER(ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)`)来获取相邻的座位信息,然后筛选出连续空余的座位并去重排序即可。
67+
6068
<!-- tabs:start -->
6169

6270
### **SQL**
@@ -85,4 +93,22 @@ FROM T
8593
WHERE a = 2 OR b = 2;
8694
```
8795

96+
```sql
97+
# Write your MySQL query statement below
98+
WITH
99+
T AS (
100+
SELECT
101+
*,
102+
sum(free = 1) OVER (
103+
ORDER BY seat_id
104+
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
105+
) AS cnt
106+
FROM Cinema
107+
)
108+
SELECT seat_id
109+
FROM T
110+
WHERE free = 1 AND cnt > 1
111+
ORDER BY 1;
112+
```
113+
88114
<!-- tabs:end -->

solution/0600-0699/0603.Consecutive Available Seats/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ Cinema table:
5454

5555
## Solutions
5656

57+
**Solution 1: Self-Join**
58+
59+
We can use a self-join to join the `Seat` table with itself, and then filter out the records where the `id` of the left seat is equal to the `id` of the right seat minus $1$, and where both seats are empty.
60+
61+
**Solution 2: Window Function**
62+
63+
We can use the `LAG` and `LEAD` functions (or `SUM() OVER(ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)`) to obtain the information of adjacent seats, and then filter out the consecutive empty seats and sort them in a unique way.
64+
5765
<!-- tabs:start -->
5866

5967
### **SQL**
@@ -82,4 +90,22 @@ FROM T
8290
WHERE a = 2 OR b = 2;
8391
```
8492

93+
```sql
94+
# Write your MySQL query statement below
95+
WITH
96+
T AS (
97+
SELECT
98+
*,
99+
sum(free = 1) OVER (
100+
ORDER BY seat_id
101+
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
102+
) AS cnt
103+
FROM Cinema
104+
)
105+
SELECT seat_id
106+
FROM T
107+
WHERE free = 1 AND cnt > 1
108+
ORDER BY 1;
109+
```
110+
85111
<!-- tabs:end -->

solution/0600-0699/0603.Consecutive Available Seats/Solution.sql

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
WITH
33
T AS (
44
SELECT
5-
seat_id,
6-
(free + (lag(free) OVER (ORDER BY seat_id))) AS a,
7-
(free + (lead(free) OVER (ORDER BY seat_id))) AS b
5+
*,
6+
sum(free = 1) OVER (
7+
ORDER BY seat_id
8+
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
9+
) AS cnt
810
FROM Cinema
911
)
1012
SELECT seat_id
1113
FROM T
12-
WHERE a = 2 OR b = 2;
14+
WHERE free = 1 AND cnt > 1
15+
ORDER BY 1;

solution/0600-0699/0613.Shortest Distance in a Line/README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,24 @@ Point 表:
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:自连接**
59+
60+
我们可以使用自连接,将表中的每个点与其他更大的点进行连接,然后计算两点之间的距离,最后取最小值。
61+
62+
**方法二:窗口函数**
63+
64+
我们也可以使用窗口函数,将表中的点按照 $x$ 排序,然后计算相邻两点之间的距离,最后取最小值。
65+
5866
<!-- tabs:start -->
5967

6068
### **SQL**
6169

6270
```sql
6371
# Write your MySQL query statement below
64-
SELECT
65-
min(abs(p1.x - p2.x)) AS shortest
72+
SELECT min(p2.x - p1.x) AS shortest
6673
FROM
6774
Point AS p1
68-
JOIN Point AS p2 ON p1.x != p2.x;
75+
JOIN Point AS p2 ON p1.x < p2.x;
6976
```
7077

7178
```sql

solution/0600-0699/0613.Shortest Distance in a Line/README_EN.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,24 @@ Point table:
4949

5050
## Solutions
5151

52+
**Solution 1: Self-Join**
53+
54+
We can use a self-join to join each point in the table with the larger points, and then calculate the distance between the two points. Finally, we can take the minimum distance.
55+
56+
**Solution 2: Window Function**
57+
58+
We can use a window function to sort the points in the table by their $x$ values, and then calculate the distance between adjacent points. Finally, we can take the minimum distance.
59+
5260
<!-- tabs:start -->
5361

5462
### **SQL**
5563

5664
```sql
5765
# Write your MySQL query statement below
58-
SELECT
59-
min(abs(p1.x - p2.x)) AS shortest
66+
SELECT min(p2.x - p1.x) AS shortest
6067
FROM
6168
Point AS p1
62-
JOIN Point AS p2 ON p1.x != p2.x;
69+
JOIN Point AS p2 ON p1.x < p2.x;
6370
```
6471

6572
```sql
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Write your MySQL query statement below
2-
SELECT x - lag(x) OVER (ORDER BY x) AS shortest
3-
FROM Point
4-
ORDER BY 1
5-
LIMIT 1, 1;
2+
SELECT min(p2.x - p1.x) AS shortest
3+
FROM
4+
Point AS p1
5+
JOIN Point AS p2 ON p1.x < p2.x;

solution/1500-1599/1501.Countries You Can Safely Invest In/README.md

+30-21
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,43 @@ Calls 表:
122122

123123
<!-- 这里可写通用的实现逻辑 -->
124124

125+
**方法一:等值连接 + 分组 + 子查询**
126+
127+
我们可以使用等值连接,将 `Person` 表和 `Calls` 表连接起来,连接的条件是 `Person.id = Calls.caller_id` 或者 `Person.id = Calls.callee_id`,然后再将连接后的表和 `Country` 表连接起来,连接的条件是 `left(phone_number, 3) = country_code`,最后按照国家分组,计算每个国家的平均通话时长,然后再使用子查询,找出平均通话时长大于全球平均通话时长的国家。
128+
125129
<!-- tabs:start -->
126130

127131
### **SQL**
128132

129133
```sql
130134
# Write your MySQL query statement below
131-
with t as (
132-
select
133-
left(phone_number, 3) as country_code,
134-
avg(duration) as duration
135-
from
136-
Person
137-
join Calls on id in (caller_id, callee_id)
138-
group by
139-
country_code
140-
)
141-
select
142-
c.name country
143-
from
144-
Country c
145-
join t on c.country_code = t.country_code
146-
where
147-
t.duration > (
148-
select
149-
avg(duration)
150-
from
151-
Calls
135+
SELECT country
136+
FROM
137+
(
138+
SELECT c.name AS country, avg(duration) AS duration
139+
FROM
140+
Person
141+
JOIN Calls ON id IN (caller_id, callee_id)
142+
JOIN Country AS c ON left(phone_number, 3) = country_code
143+
GROUP BY 1
144+
) AS t
145+
WHERE duration > (SELECT avg(duration) FROM Calls);
146+
```
147+
148+
```sql
149+
# Write your MySQL query statement below
150+
WITH
151+
T AS (
152+
SELECT c.name AS country, avg(duration) AS duration
153+
FROM
154+
Person
155+
JOIN Calls ON id IN (caller_id, callee_id)
156+
JOIN Country AS c ON left(phone_number, 3) = country_code
157+
GROUP BY 1
152158
)
159+
SELECT country
160+
FROM T
161+
WHERE duration > (SELECT avg(duration) FROM Calls);
153162
```
154163

155164
<!-- tabs:end -->

solution/1500-1599/1501.Countries You Can Safely Invest In/README_EN.md

+30-21
Original file line numberDiff line numberDiff line change
@@ -117,34 +117,43 @@ Since Peru is the only country where the average call duration is greater than t
117117

118118
## Solutions
119119

120+
**Solution 1: Equi-Join + Group By + Subquery**
121+
122+
We can use an equi-join to join the `Person` table and the `Calls` table on the condition of `Person.id = Calls.caller_id` or `Person.id = Calls.callee_id`, and then join the result with the `Country` table on the condition of `left(phone_number, 3) = country_code`. After that, we can group by country and calculate the average call duration for each country. Finally, we can use a subquery to find the countries whose average call duration is greater than the global average call duration.
123+
120124
<!-- tabs:start -->
121125

122126
### **SQL**
123127

124128
```sql
125129
# Write your MySQL query statement below
126-
with t as (
127-
select
128-
left(phone_number, 3) as country_code,
129-
avg(duration) as duration
130-
from
131-
Person
132-
join Calls on id in (caller_id, callee_id)
133-
group by
134-
country_code
135-
)
136-
select
137-
c.name country
138-
from
139-
Country c
140-
join t on c.country_code = t.country_code
141-
where
142-
t.duration > (
143-
select
144-
avg(duration)
145-
from
146-
Calls
130+
SELECT country
131+
FROM
132+
(
133+
SELECT c.name AS country, avg(duration) AS duration
134+
FROM
135+
Person
136+
JOIN Calls ON id IN (caller_id, callee_id)
137+
JOIN Country AS c ON left(phone_number, 3) = country_code
138+
GROUP BY 1
139+
) AS t
140+
WHERE duration > (SELECT avg(duration) FROM Calls);
141+
```
142+
143+
```sql
144+
# Write your MySQL query statement below
145+
WITH
146+
T AS (
147+
SELECT c.name AS country, avg(duration) AS duration
148+
FROM
149+
Person
150+
JOIN Calls ON id IN (caller_id, callee_id)
151+
JOIN Country AS c ON left(phone_number, 3) = country_code
152+
GROUP BY 1
147153
)
154+
SELECT country
155+
FROM T
156+
WHERE duration > (SELECT avg(duration) FROM Calls);
148157
```
149158

150159
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
# Write your MySQL query statement below
22
WITH
3-
t AS (
4-
SELECT
5-
left(phone_number, 3) AS country_code,
6-
avg(duration) AS duration
3+
T AS (
4+
SELECT c.name AS country, avg(duration) AS duration
75
FROM
86
Person
97
JOIN Calls ON id IN (caller_id, callee_id)
10-
GROUP BY country_code
8+
JOIN Country AS c ON left(phone_number, 3) = country_code
9+
GROUP BY 1
1110
)
12-
SELECT
13-
c.name AS country
14-
FROM
15-
Country AS c
16-
JOIN t ON c.country_code = t.country_code
17-
WHERE
18-
t.duration > (
19-
SELECT
20-
avg(duration)
21-
FROM Calls
22-
);
11+
SELECT country
12+
FROM T
13+
WHERE duration > (SELECT avg(duration) FROM Calls);

solution/1700-1799/1795.Rearrange Products Table/README.md

+7-41
Original file line numberDiff line numberDiff line change
@@ -61,57 +61,23 @@ Products table:
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:合并**
65+
66+
我们可以筛选出每个商店的产品和价格,然后使用 `UNION` 合并即可。
67+
6468
<!-- tabs:start -->
6569

6670
### **SQL**
6771

6872
<!-- 这里可写当前语言的特殊实现逻辑 -->
6973

70-
```sql
71-
SELECT
72-
product_id,
73-
'store1' AS store,
74-
store1 AS price
75-
FROM Products
76-
WHERE store1 IS NOT NULL
77-
UNION
78-
SELECT
79-
product_id,
80-
'store2' AS store,
81-
store2 AS price
82-
FROM Products
83-
WHERE store2 IS NOT NULL
84-
UNION
85-
SELECT
86-
product_id,
87-
'store3' AS store,
88-
store3 AS price
89-
FROM Products
90-
WHERE store3 IS NOT NULL;
91-
```
92-
9374
```sql
9475
# Write your MySQL query statement below
95-
SELECT
96-
product_id,
97-
'store1' AS store,
98-
store1 AS price
99-
FROM Products
100-
WHERE store1 > 0
76+
SELECT product_id, 'store1' AS store, store1 AS price FROM Products WHERE store1 IS NOT NULL
10177
UNION
102-
SELECT
103-
product_id,
104-
'store2' AS store,
105-
store2 AS price
106-
FROM Products
107-
WHERE store2 > 0
78+
SELECT product_id, 'store2' AS store, store2 AS price FROM Products WHERE store2 IS NOT NULL
10879
UNION
109-
SELECT
110-
product_id,
111-
'store3' AS store,
112-
store3 AS price
113-
FROM Products
114-
WHERE store3 > 0;
80+
SELECT product_id, 'store3' AS store, store3 AS price FROM Products WHERE store3 IS NOT NULL;
11581
```
11682

11783
<!-- tabs:end -->

0 commit comments

Comments
 (0)