Skip to content

Commit afa7067

Browse files
authored
feat: add solutions to lc problems: No.3050,3051 (doocs#2382)
* No.3050.Pizza Toppings Cost Analysis * No.3051.Find Candidates for Data Scientist Position
1 parent 7043fa6 commit afa7067

File tree

6 files changed

+79
-8
lines changed

6 files changed

+79
-8
lines changed

solution/3000-3099/3050.Pizza Toppings Cost Analysis/README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,29 @@ Output table is ordered by the total cost in descending order.</pre>
6767

6868
## 解法
6969

70-
### 方法一
70+
### 方法一:窗口函数 + 条件连接
71+
72+
我们先使用窗口函数,按照 `topping_name` 字段对表进行排序,并为每一行添加一个 `rk` 字段,表示当前行的排名。
73+
74+
然后我们使用条件连接,连接三次表 `T`,分别为 `t1`, `t2`, `t3`。连接条件是 `t1.rk < t2.rk``t2.rk < t3.rk`。然后我们计算三个配料的总价,按照总价降序排序,再按照配料名升序排序。
7175

7276
<!-- tabs:start -->
7377

7478
```sql
75-
79+
# Write your MySQL query statement below
80+
WITH
81+
T AS (
82+
SELECT *, RANK() OVER (ORDER BY topping_name) AS rk
83+
FROM Toppings
84+
)
85+
SELECT
86+
CONCAT(t1.topping_name, ',', t2.topping_name, ',', t3.topping_name) AS pizza,
87+
t1.cost + t2.cost + t3.cost AS total_cost
88+
FROM
89+
T AS t1
90+
JOIN T AS t2 ON t1.rk < t2.rk
91+
JOIN T AS t3 ON t2.rk < t3.rk
92+
ORDER BY 2 DESC, 1 ASC;
7693
```
7794

7895
<!-- tabs:end -->

solution/3000-3099/3050.Pizza Toppings Cost Analysis/README_EN.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,29 @@ Output table is ordered by the total cost in descending order.</pre>
6565

6666
## Solutions
6767

68-
### Solution 1
68+
### Solution 1: Window Function + Conditional Join
69+
70+
First, we use a window function to sort the table by the `topping_name` field and add a `rk` field to each row, representing the ranking of the current row.
71+
72+
Then we use conditional join to join the table `T` three times, named as `t1`, `t2`, `t3` respectively. The join conditions are `t1.rk < t2.rk` and `t2.rk < t3.rk`. After that, we calculate the total price of the three toppings, sort by total price in descending order, and then sort by topping name in ascending order.
6973

7074
<!-- tabs:start -->
7175

7276
```sql
73-
77+
# Write your MySQL query statement below
78+
WITH
79+
T AS (
80+
SELECT *, RANK() OVER (ORDER BY topping_name) AS rk
81+
FROM Toppings
82+
)
83+
SELECT
84+
CONCAT(t1.topping_name, ',', t2.topping_name, ',', t3.topping_name) AS pizza,
85+
t1.cost + t2.cost + t3.cost AS total_cost
86+
FROM
87+
T AS t1
88+
JOIN T AS t2 ON t1.rk < t2.rk
89+
JOIN T AS t3 ON t2.rk < t3.rk
90+
ORDER BY 2 DESC, 1 ASC;
7491
```
7592

7693
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT *, RANK() OVER (ORDER BY topping_name) AS rk
5+
FROM Toppings
6+
)
7+
SELECT
8+
CONCAT(t1.topping_name, ',', t2.topping_name, ',', t3.topping_name) AS pizza,
9+
t1.cost + t2.cost + t3.cost AS total_cost
10+
FROM
11+
T AS t1
12+
JOIN T AS t2 ON t1.rk < t2.rk
13+
JOIN T AS t3 ON t2.rk < t3.rk
14+
ORDER BY 2 DESC, 1 ASC;

solution/3000-3099/3051.Find Candidates for Data Scientist Position/README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,20 @@ The output table is sorted by candidate_id in ascending order.
6565

6666
## 解法
6767

68-
### 方法一
68+
### 方法一:条件筛选 + 分组统计
69+
70+
我们首先筛选出具备 `Python`, `Tableau`, `PostgreSQL` 这三个技能的候选人,然后按照 `candidate_id` 进行分组统计,统计每个候选人具备的技能数量,最后筛选出具备这三个技能的候选人,并且按照 `candidate_id` 进行升序排序。
6971

7072
<!-- tabs:start -->
7173

7274
```sql
73-
75+
# Write your MySQL query statement below
76+
SELECT candidate_id
77+
FROM Candidates
78+
WHERE skill IN ('Python', 'Tableau', 'PostgreSQL')
79+
GROUP BY 1
80+
HAVING COUNT(1) = 3
81+
ORDER BY 1;
7482
```
7583

7684
<!-- tabs:end -->

solution/3000-3099/3051.Find Candidates for Data Scientist Position/README_EN.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,20 @@ The output table is sorted by candidate_id in ascending order.
6363

6464
## Solutions
6565

66-
### Solution 1
66+
### Solution 1: Conditional Filtering + Grouping Statistics
67+
68+
First, we filter out candidates who have the skills `Python`, `Tableau`, and `PostgreSQL`. Then, we group by `candidate_id` and count the number of skills each candidate has. Finally, we filter out candidates who have these three skills and sort them in ascending order by `candidate_id`.
6769

6870
<!-- tabs:start -->
6971

7072
```sql
71-
73+
# Write your MySQL query statement below
74+
SELECT candidate_id
75+
FROM Candidates
76+
WHERE skill IN ('Python', 'Tableau', 'PostgreSQL')
77+
GROUP BY 1
78+
HAVING COUNT(1) = 3
79+
ORDER BY 1;
7280
```
7381

7482
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
SELECT candidate_id
3+
FROM Candidates
4+
WHERE skill IN ('Python', 'Tableau', 'PostgreSQL')
5+
GROUP BY 1
6+
HAVING COUNT(1) = 3
7+
ORDER BY 1;

0 commit comments

Comments
 (0)