Skip to content

Commit 88aa72d

Browse files
authored
feat: add solutions to lc problems (doocs#1190)
1 parent 3da8b15 commit 88aa72d

File tree

27 files changed

+459
-319
lines changed

27 files changed

+459
-319
lines changed

solution/0600-0699/0601.Human Traffic of Stadium/README.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,18 @@ id</strong> 为 5、6、7、8 的四行 id 连续,并且每行都有 &gt;= 100
7373
```sql
7474
# Write your MySQL query statement below
7575
WITH
76-
s AS (
77-
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
76+
S AS (
77+
SELECT
78+
*,
79+
id - (row_number() OVER (ORDER BY id)) AS rk
7880
FROM Stadium
7981
WHERE people >= 100
8082
),
81-
t AS (
82-
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
83-
FROM s
84-
)
83+
T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S)
8584
SELECT id, visit_date, people
86-
FROM t
85+
FROM T
8786
WHERE cnt >= 3
88-
ORDER BY visit_date;
87+
ORDER BY 1;
8988
```
9089

9190
<!-- tabs:end -->

solution/0600-0699/0601.Human Traffic of Stadium/README_EN.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,18 @@ The rows with ids 2 and 3 are not included because we need at least three consec
6868
```sql
6969
# Write your MySQL query statement below
7070
WITH
71-
s AS (
72-
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
71+
S AS (
72+
SELECT
73+
*,
74+
id - (row_number() OVER (ORDER BY id)) AS rk
7375
FROM Stadium
7476
WHERE people >= 100
7577
),
76-
t AS (
77-
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
78-
FROM s
79-
)
78+
T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S)
8079
SELECT id, visit_date, people
81-
FROM t
80+
FROM T
8281
WHERE cnt >= 3
83-
ORDER BY visit_date;
82+
ORDER BY 1;
8483
```
8584

8685
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
# Write your MySQL query statement below
22
WITH
3-
s AS (
4-
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
3+
S AS (
4+
SELECT
5+
*,
6+
id - (row_number() OVER (ORDER BY id)) AS rk
57
FROM Stadium
68
WHERE people >= 100
79
),
8-
t AS (
9-
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
10-
FROM s
11-
)
10+
T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S)
1211
SELECT id, visit_date, people
13-
FROM t
12+
FROM T
1413
WHERE cnt >= 3
15-
ORDER BY visit_date;
14+
ORDER BY 1;

solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README.md

+11-15
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,17 @@ RequestAccepted 表:
7171
### **SQL**
7272

7373
```sql
74-
SELECT
75-
ids AS id,
76-
COUNT(*) AS num
77-
FROM
78-
(
79-
SELECT
80-
requester_id AS ids
81-
FROM RequestAccepted
82-
UNION ALL
83-
SELECT
84-
accepter_id
85-
FROM RequestAccepted
86-
) AS t
87-
GROUP BY ids
88-
ORDER BY num DESC
74+
# Write your MySQL query statement below
75+
WITH
76+
T AS (
77+
SELECT requester_id, accepter_id FROM RequestAccepted
78+
UNION
79+
SELECT accepter_id, requester_id FROM RequestAccepted
80+
)
81+
SELECT requester_id AS id, count(accepter_id) AS num
82+
FROM T
83+
GROUP BY 1
84+
ORDER BY 2 DESC
8985
LIMIT 1;
9086
```
9187

solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README_EN.md

+11-15
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,17 @@ The person with id 3 is a friend of people 1, 2, and 4, so he has three friends
6060
### **SQL**
6161

6262
```sql
63-
SELECT
64-
ids AS id,
65-
COUNT(*) AS num
66-
FROM
67-
(
68-
SELECT
69-
requester_id AS ids
70-
FROM RequestAccepted
71-
UNION ALL
72-
SELECT
73-
accepter_id
74-
FROM RequestAccepted
75-
) AS t
76-
GROUP BY ids
77-
ORDER BY num DESC
63+
# Write your MySQL query statement below
64+
WITH
65+
T AS (
66+
SELECT requester_id, accepter_id FROM RequestAccepted
67+
UNION
68+
SELECT accepter_id, requester_id FROM RequestAccepted
69+
)
70+
SELECT requester_id AS id, count(accepter_id) AS num
71+
FROM T
72+
GROUP BY 1
73+
ORDER BY 2 DESC
7874
LIMIT 1;
7975
```
8076

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
SELECT
2-
ids AS id,
3-
COUNT(*) AS num
4-
FROM
5-
(
6-
SELECT
7-
requester_id AS ids
8-
FROM RequestAccepted
9-
UNION ALL
10-
SELECT
11-
accepter_id
12-
FROM RequestAccepted
13-
) AS t
14-
GROUP BY ids
15-
ORDER BY num DESC
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT requester_id, accepter_id FROM RequestAccepted
5+
UNION
6+
SELECT accepter_id, requester_id FROM RequestAccepted
7+
)
8+
SELECT requester_id AS id, count(accepter_id) AS num
9+
FROM T
10+
GROUP BY 1
11+
ORDER BY 2 DESC
1612
LIMIT 1;

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,23 @@ Cinema 表:
6666
SELECT DISTINCT a.seat_id
6767
FROM
6868
Cinema AS a
69-
JOIN Cinema AS b
70-
ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE
71-
ORDER BY a.seat_id;
69+
JOIN Cinema AS b ON abs(a.seat_id - b.seat_id) = 1 AND a.free AND b.free
70+
ORDER BY 1;
71+
```
72+
73+
```sql
74+
# Write your MySQL query statement below
75+
WITH
76+
T AS (
77+
SELECT
78+
seat_id,
79+
(free + (lag(free) OVER (ORDER BY seat_id))) AS a,
80+
(free + (lead(free) OVER (ORDER BY seat_id))) AS b
81+
FROM Cinema
82+
)
83+
SELECT seat_id
84+
FROM T
85+
WHERE a = 2 OR b = 2;
7286
```
7387

7488
<!-- tabs:end -->

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,23 @@ Cinema table:
6363
SELECT DISTINCT a.seat_id
6464
FROM
6565
Cinema AS a
66-
JOIN Cinema AS b
67-
ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE
68-
ORDER BY a.seat_id;
66+
JOIN Cinema AS b ON abs(a.seat_id - b.seat_id) = 1 AND a.free AND b.free
67+
ORDER BY 1;
68+
```
69+
70+
```sql
71+
# Write your MySQL query statement below
72+
WITH
73+
T AS (
74+
SELECT
75+
seat_id,
76+
(free + (lag(free) OVER (ORDER BY seat_id))) AS a,
77+
(free + (lead(free) OVER (ORDER BY seat_id))) AS b
78+
FROM Cinema
79+
)
80+
SELECT seat_id
81+
FROM T
82+
WHERE a = 2 OR b = 2;
6983
```
7084

7185
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# Write your MySQL query statement below
2-
SELECT DISTINCT a.seat_id
3-
FROM
4-
Cinema AS a
5-
JOIN Cinema AS b
6-
ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE
7-
ORDER BY a.seat_id;
2+
WITH
3+
T AS (
4+
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
8+
FROM Cinema
9+
)
10+
SELECT seat_id
11+
FROM T
12+
WHERE a = 2 OR b = 2;

solution/0600-0699/0607.Sales Person/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,19 @@ WHERE
149149
);
150150
```
151151

152+
```sql
153+
# Write your MySQL query statement below
154+
SELECT name
155+
FROM SalesPerson
156+
WHERE
157+
sales_id NOT IN (
158+
SELECT sales_id
159+
FROM
160+
Company AS c
161+
JOIN Orders AS o USING (com_id)
162+
GROUP BY sales_id
163+
HAVING sum(name = 'RED') > 0
164+
);
165+
```
166+
152167
<!-- tabs:end -->

solution/0600-0699/0607.Sales Person/README_EN.md

+15
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,19 @@ WHERE
144144
);
145145
```
146146

147+
```sql
148+
# Write your MySQL query statement below
149+
SELECT name
150+
FROM SalesPerson
151+
WHERE
152+
sales_id NOT IN (
153+
SELECT sales_id
154+
FROM
155+
Company AS c
156+
JOIN Orders AS o USING (com_id)
157+
GROUP BY sales_id
158+
HAVING sum(name = 'RED') > 0
159+
);
160+
```
161+
147162
<!-- tabs:end -->

solution/0600-0699/0608.Tree Node/README.md

+3-23
Original file line numberDiff line numberDiff line change
@@ -79,36 +79,16 @@
7979

8080
### **SQL**
8181

82-
```sql
83-
SELECT
84-
id,
85-
(
86-
CASE
87-
WHEN p_id IS NULL THEN 'Root'
88-
WHEN id IN (
89-
SELECT p_id
90-
FROM tree
91-
) THEN 'Inner'
92-
ELSE 'Leaf'
93-
END
94-
) AS type
95-
FROM tree;
96-
```
97-
9882
```sql
9983
# Write your MySQL query statement below
10084
SELECT
10185
id,
10286
CASE
10387
WHEN p_id IS NULL THEN 'Root'
104-
WHEN id IN (
105-
SELECT
106-
p_id
107-
FROM tree
108-
) THEN 'Inner'
88+
WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
10989
ELSE 'Leaf'
110-
END AS Type
111-
FROM tree;
90+
END AS type
91+
FROM Tree;
11292
```
11393

11494
<!-- tabs:end -->

solution/0600-0699/0608.Tree Node/README_EN.md

+3-23
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,16 @@ Tree table:
9090

9191
### **SQL**
9292

93-
```sql
94-
SELECT
95-
id,
96-
(
97-
CASE
98-
WHEN p_id IS NULL THEN 'Root'
99-
WHEN id IN (
100-
SELECT p_id
101-
FROM tree
102-
) THEN 'Inner'
103-
ELSE 'Leaf'
104-
END
105-
) AS type
106-
FROM tree;
107-
```
108-
10993
```sql
11094
# Write your MySQL query statement below
11195
SELECT
11296
id,
11397
CASE
11498
WHEN p_id IS NULL THEN 'Root'
115-
WHEN id IN (
116-
SELECT
117-
p_id
118-
FROM tree
119-
) THEN 'Inner'
99+
WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
120100
ELSE 'Leaf'
121-
END AS Type
122-
FROM tree;
101+
END AS type
102+
FROM Tree;
123103
```
124104

125105
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1+
# Write your MySQL query statement below
12
SELECT
23
id,
3-
(
4-
CASE
5-
WHEN p_id IS NULL THEN 'Root'
6-
WHEN id IN (
7-
SELECT p_id
8-
FROM tree
9-
) THEN 'Inner'
10-
ELSE 'Leaf'
11-
END
12-
) AS type
13-
FROM tree;
4+
CASE
5+
WHEN p_id IS NULL THEN 'Root'
6+
WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
7+
ELSE 'Leaf'
8+
END AS type
9+
FROM Tree;

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

+8
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ FROM
5959
JOIN Point AS p2 ON p1.x != p2.x;
6060
```
6161

62+
```sql
63+
# Write your MySQL query statement below
64+
SELECT x - lag(x) OVER (ORDER BY x) AS shortest
65+
FROM Point
66+
ORDER BY 1
67+
LIMIT 1, 1;
68+
```
69+
6270
<!-- tabs:end -->

0 commit comments

Comments
 (0)