Skip to content

Commit 0c7a735

Browse files
authored
feat: update sql solutions to lc problems (#1188)
* No.0585.Investments in 2016 * No.0586.Customer Placing the Largest Number of Orders * No.0595.Big Countries * No.0596.Classes More Than 5 Students * No.0597.Friend Requests I Overall Acceptance Rate
1 parent f1f56bc commit 0c7a735

File tree

16 files changed

+99
-82
lines changed

16 files changed

+99
-82
lines changed

solution/0000-0099/0016.3Sum Closest/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
**方法一:排序 + 双指针**
4747

48-
将数组排序,然后遍历数组,对于每个元素 $nums[i]$,我们使用指针 $j$ 和 $k$ 分别指向 $i+1$ 和 $n-1$,计算三数之和,如果三数之和等于 $target$,则直接返回 $target$,否则根据与 $target$ 的差值更新答案。如果三数之和大于 $target$,则将 $k$ 向左移动一位,否则将 $j$ 向右移动一位。
48+
我们将数组排序,然后遍历数组,对于每个元素 $nums[i]$,我们使用指针 $j$ 和 $k$ 分别指向 $i+1$ 和 $n-1$,计算三数之和,如果三数之和等于 $target$,则直接返回 $target$,否则根据与 $target$ 的差值更新答案。如果三数之和大于 $target$,则将 $k$ 向左移动一位,否则将 $j$ 向右移动一位。
4949

5050
时间复杂度 $O(n^2)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。
5151

solution/0500-0599/0585.Investments in 2016/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,16 @@ tiv_2015 值为 10 与第三条和第四条记录相同,且其位置是唯一
8080
```sql
8181
# Write your MySQL query statement below
8282
WITH
83-
t AS (
83+
T AS (
8484
SELECT
8585
tiv_2016,
8686
count(pid) OVER (PARTITION BY tiv_2015) AS cnt1,
87-
count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2
87+
count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2
8888
FROM Insurance
8989
)
90-
SELECT
91-
round(sum(TIV_2016), 2) AS tiv_2016
92-
FROM t
93-
WHERE cnt1 != 1 AND cnt2 = 1;
90+
SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016
91+
FROM T
92+
WHERE cnt1 > 1 AND cnt2 = 1;
9493
```
9594

9695
<!-- tabs:end -->

solution/0500-0599/0585.Investments in 2016/README_EN.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,16 @@ So, the result is the sum of tiv_2016 of the first and last record, which is 45.
7474
```sql
7575
# Write your MySQL query statement below
7676
WITH
77-
t AS (
77+
T AS (
7878
SELECT
7979
tiv_2016,
8080
count(pid) OVER (PARTITION BY tiv_2015) AS cnt1,
81-
count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2
81+
count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2
8282
FROM Insurance
8383
)
84-
SELECT
85-
round(sum(TIV_2016), 2) AS tiv_2016
86-
FROM t
87-
WHERE cnt1 != 1 AND cnt2 = 1;
84+
SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016
85+
FROM T
86+
WHERE cnt1 > 1 AND cnt2 = 1;
8887
```
8988

9089
<!-- tabs:end -->
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Write your MySQL query statement below
22
WITH
3-
t AS (
3+
T AS (
44
SELECT
55
tiv_2016,
66
count(pid) OVER (PARTITION BY tiv_2015) AS cnt1,
7-
count(pid) OVER (PARTITION BY concat(lat, lon)) AS cnt2
7+
count(pid) OVER (PARTITION BY concat(lat, '-', lon)) AS cnt2
88
FROM Insurance
99
)
10-
SELECT
11-
round(sum(TIV_2016), 2) AS tiv_2016
12-
FROM t
13-
WHERE cnt1 != 1 AND cnt2 = 1;
10+
SELECT round(ifnull(sum(tiv_2016), 0), 2) AS tiv_2016
11+
FROM T
12+
WHERE cnt1 > 1 AND cnt2 = 1;

solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ ORDER BY count(1) DESC
7575
LIMIT 1;
7676
```
7777

78-
SQL Server
79-
8078
```sql
79+
/* Write your T-SQL query statement below */
8180
SELECT TOP 1
8281
customer_number
8382
FROM

solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,15 @@ So the result is customer_number 3.
6161

6262
```sql
6363
# Write your MySQL query statement below
64-
SELECT
65-
customer_number
66-
FROM orders
67-
GROUP BY customer_number
64+
SELECT customer_number
65+
FROM Orders
66+
GROUP BY 1
6867
ORDER BY count(1) DESC
6968
LIMIT 1;
7069
```
7170

72-
SQL Server
73-
7471
```sql
72+
/* Write your T-SQL query statement below */
7573
SELECT TOP 1
7674
customer_number
7775
FROM
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
customer_number
4-
FROM orders
5-
GROUP BY customer_number
2+
SELECT customer_number
3+
FROM Orders
4+
GROUP BY 1
65
ORDER BY count(1) DESC
76
LIMIT 1;

solution/0500-0599/0595.Big Countries/README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,34 @@ World 表:
7272

7373
<!-- 这里可写通用的实现逻辑 -->
7474

75+
**方法一:使用 WHERE + OR**
76+
77+
我们可以使用 `WHERE` + `OR` 查询出所有符合条件的国家。
78+
79+
**方法二:使用 UNION**
80+
81+
我们可以查询出所有面积大于等于 300 万平方公里的国家,然后再查询出所有人口大于等于 2500 万的国家,最后使用 `UNION` 将两个结果集合并起来。
82+
7583
<!-- tabs:start -->
7684

7785
### **SQL**
7886

7987
```sql
80-
SELECT
81-
name,
82-
population,
83-
area
84-
FROM world
85-
WHERE area > 3000000 OR population > 25000000;
88+
# Write your MySQL query statement below
89+
SELECT name, population, area
90+
FROM World
91+
WHERE area >= 3000000 OR population >= 25000000;
92+
```
93+
94+
```sql
95+
# Write your MySQL query statement below
96+
SELECT name, population, area
97+
FROM World
98+
WHERE area >= 3000000
99+
UNION
100+
SELECT name, population, area
101+
FROM World
102+
WHERE population >= 25000000;
86103
```
87104

88105
<!-- tabs:end -->

solution/0500-0599/0595.Big Countries/README_EN.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,21 @@ World table:
6666
### **SQL**
6767

6868
```sql
69-
SELECT
70-
name,
71-
population,
72-
area
73-
FROM world
74-
WHERE area > 3000000 OR population > 25000000;
69+
# Write your MySQL query statement below
70+
SELECT name, population, area
71+
FROM World
72+
WHERE area >= 3000000 OR population >= 25000000;
73+
```
74+
75+
```sql
76+
# Write your MySQL query statement below
77+
SELECT name, population, area
78+
FROM World
79+
WHERE area >= 3000000
80+
UNION
81+
SELECT name, population, area
82+
FROM World
83+
WHERE population >= 25000000;
7584
```
7685

7786
<!-- tabs:end -->
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
SELECT
2-
name,
3-
population,
4-
area
5-
FROM world
6-
WHERE area > 3000000 OR population > 25000000;
1+
# Write your MySQL query statement below
2+
SELECT name, population, area
3+
FROM World
4+
WHERE area >= 3000000 OR population >= 25000000;

0 commit comments

Comments
 (0)