Skip to content

feat: add solutions to lc problems: No.3050,3051 #2382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions solution/3000-3099/3050.Pizza Toppings Cost Analysis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,29 @@ Output table is ordered by the total cost in descending order.</pre>

## 解法

### 方法一
### 方法一:窗口函数 + 条件连接

我们先使用窗口函数,按照 `topping_name` 字段对表进行排序,并为每一行添加一个 `rk` 字段,表示当前行的排名。

然后我们使用条件连接,连接三次表 `T`,分别为 `t1`, `t2`, `t3`。连接条件是 `t1.rk < t2.rk` 和 `t2.rk < t3.rk`。然后我们计算三个配料的总价,按照总价降序排序,再按照配料名升序排序。

<!-- tabs:start -->

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT *, RANK() OVER (ORDER BY topping_name) AS rk
FROM Toppings
)
SELECT
CONCAT(t1.topping_name, ',', t2.topping_name, ',', t3.topping_name) AS pizza,
t1.cost + t2.cost + t3.cost AS total_cost
FROM
T AS t1
JOIN T AS t2 ON t1.rk < t2.rk
JOIN T AS t3 ON t2.rk < t3.rk
ORDER BY 2 DESC, 1 ASC;
```

<!-- tabs:end -->
Expand Down
21 changes: 19 additions & 2 deletions solution/3000-3099/3050.Pizza Toppings Cost Analysis/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,29 @@ Output table is ordered by the total cost in descending order.</pre>

## Solutions

### Solution 1
### Solution 1: Window Function + Conditional Join

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.

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.

<!-- tabs:start -->

```sql

# Write your MySQL query statement below
WITH
T AS (
SELECT *, RANK() OVER (ORDER BY topping_name) AS rk
FROM Toppings
)
SELECT
CONCAT(t1.topping_name, ',', t2.topping_name, ',', t3.topping_name) AS pizza,
t1.cost + t2.cost + t3.cost AS total_cost
FROM
T AS t1
JOIN T AS t2 ON t1.rk < t2.rk
JOIN T AS t3 ON t2.rk < t3.rk
ORDER BY 2 DESC, 1 ASC;
```

<!-- tabs:end -->
Expand Down
14 changes: 14 additions & 0 deletions solution/3000-3099/3050.Pizza Toppings Cost Analysis/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT *, RANK() OVER (ORDER BY topping_name) AS rk
FROM Toppings
)
SELECT
CONCAT(t1.topping_name, ',', t2.topping_name, ',', t3.topping_name) AS pizza,
t1.cost + t2.cost + t3.cost AS total_cost
FROM
T AS t1
JOIN T AS t2 ON t1.rk < t2.rk
JOIN T AS t3 ON t2.rk < t3.rk
ORDER BY 2 DESC, 1 ASC;
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,20 @@ The output table is sorted by candidate_id in ascending order.

## 解法

### 方法一
### 方法一:条件筛选 + 分组统计

我们首先筛选出具备 `Python`, `Tableau`, `PostgreSQL` 这三个技能的候选人,然后按照 `candidate_id` 进行分组统计,统计每个候选人具备的技能数量,最后筛选出具备这三个技能的候选人,并且按照 `candidate_id` 进行升序排序。

<!-- tabs:start -->

```sql

# Write your MySQL query statement below
SELECT candidate_id
FROM Candidates
WHERE skill IN ('Python', 'Tableau', 'PostgreSQL')
GROUP BY 1
HAVING COUNT(1) = 3
ORDER BY 1;
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,20 @@ The output table is sorted by candidate_id in ascending order.

## Solutions

### Solution 1
### Solution 1: Conditional Filtering + Grouping Statistics

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`.

<!-- tabs:start -->

```sql

# Write your MySQL query statement below
SELECT candidate_id
FROM Candidates
WHERE skill IN ('Python', 'Tableau', 'PostgreSQL')
GROUP BY 1
HAVING COUNT(1) = 3
ORDER BY 1;
```

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