From 3eea8aec1ef99f474f5a584c107b90e6b4152819 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 15 Jul 2023 16:02:03 +0800 Subject: [PATCH] feat: add sql solutions to lc problems * No.1179.Reformat Department Table * No.1193.Monthly Transactions I * No.1204.Last Person to Fit in the Bus * No.1205.Monthly Transactions II * No.1205.Monthly Transactions II * No.2772.Apply Operations to Make All Array Elements Equal to Zero --- .../1179.Reformat Department Table/README.md | 2 +- .../README_EN.md | 2 +- .../Solution.sql | 2 +- .../1193.Monthly Transactions I/README.md | 16 +++++++-------- .../1193.Monthly Transactions I/README_EN.md | 16 +++++++-------- .../1193.Monthly Transactions I/Solution.sql | 12 +++++------ .../README.md | 16 +++++++++++++++ .../README_EN.md | 16 +++++++++++++++ .../Solution.sql | 19 ++++++++++-------- .../1205.Monthly Transactions II/README.md | 12 +++++------ .../1205.Monthly Transactions II/README_EN.md | 12 +++++------ .../1205.Monthly Transactions II/Solution.sql | 12 +++++------ .../README_EN.md | 20 +++++++++---------- 13 files changed, 96 insertions(+), 61 deletions(-) diff --git a/solution/1100-1199/1179.Reformat Department Table/README.md b/solution/1100-1199/1179.Reformat Department Table/README.md index a2a97a53302b5..6628f4157b2c2 100644 --- a/solution/1100-1199/1179.Reformat Department Table/README.md +++ b/solution/1100-1199/1179.Reformat Department Table/README.md @@ -124,7 +124,7 @@ SELECT END ) AS Dec_Revenue FROM Department -GROUP BY id; +GROUP BY 1; ``` diff --git a/solution/1100-1199/1179.Reformat Department Table/README_EN.md b/solution/1100-1199/1179.Reformat Department Table/README_EN.md index 2802fa5ba7b14..7df5cac58dcbf 100644 --- a/solution/1100-1199/1179.Reformat Department Table/README_EN.md +++ b/solution/1100-1199/1179.Reformat Department Table/README_EN.md @@ -125,7 +125,7 @@ SELECT END ) AS Dec_Revenue FROM Department -GROUP BY id; +GROUP BY 1; ``` diff --git a/solution/1100-1199/1179.Reformat Department Table/Solution.sql b/solution/1100-1199/1179.Reformat Department Table/Solution.sql index 23e99309d145f..c91f0cafddf12 100644 --- a/solution/1100-1199/1179.Reformat Department Table/Solution.sql +++ b/solution/1100-1199/1179.Reformat Department Table/Solution.sql @@ -62,4 +62,4 @@ SELECT END ) AS Dec_Revenue FROM Department -GROUP BY id; +GROUP BY 1; diff --git a/solution/1100-1199/1193.Monthly Transactions I/README.md b/solution/1100-1199/1193.Monthly Transactions I/README.md index 689d231e32878..91f7aa009a05b 100644 --- a/solution/1100-1199/1193.Monthly Transactions I/README.md +++ b/solution/1100-1199/1193.Monthly Transactions I/README.md @@ -65,15 +65,15 @@ Transactions table: ```sql # Write your MySQL query statement below -SELECT DATE_FORMAT(trans_date,'%Y-%m') AS month - ,country - ,COUNT(*) AS trans_count - ,COUNT(IF(state = 'approved',1,NULL)) AS approved_count - ,SUM(amount) AS trans_total_amount - ,SUM(IF(state = 'approved',amount,0)) AS approved_total_amount +SELECT + date_format(trans_date, '%Y-%m') AS month, + country, + count(1) AS trans_count, + sum(state = 'approved') AS approved_count, + sum(amount) AS trans_total_amount, + sum(if(state = 'approved', amount, 0)) AS approved_total_amount FROM Transactions -GROUP BY month - ,country +GROUP BY 1, 2; ``` diff --git a/solution/1100-1199/1193.Monthly Transactions I/README_EN.md b/solution/1100-1199/1193.Monthly Transactions I/README_EN.md index d568a03cb5073..01c46ca79c6d2 100644 --- a/solution/1100-1199/1193.Monthly Transactions I/README_EN.md +++ b/solution/1100-1199/1193.Monthly Transactions I/README_EN.md @@ -61,15 +61,15 @@ Transactions table: ```sql # Write your MySQL query statement below -SELECT DATE_FORMAT(trans_date,'%Y-%m') AS month - ,country - ,COUNT(*) AS trans_count - ,COUNT(IF(state = 'approved',1,NULL)) AS approved_count - ,SUM(amount) AS trans_total_amount - ,SUM(IF(state = 'approved',amount,0)) AS approved_total_amount +SELECT + date_format(trans_date, '%Y-%m') AS month, + country, + count(1) AS trans_count, + sum(state = 'approved') AS approved_count, + sum(amount) AS trans_total_amount, + sum(if(state = 'approved', amount, 0)) AS approved_total_amount FROM Transactions -GROUP BY month - ,country +GROUP BY 1, 2; ``` diff --git a/solution/1100-1199/1193.Monthly Transactions I/Solution.sql b/solution/1100-1199/1193.Monthly Transactions I/Solution.sql index 0d160aa9c1ee9..3da7c133a1832 100644 --- a/solution/1100-1199/1193.Monthly Transactions I/Solution.sql +++ b/solution/1100-1199/1193.Monthly Transactions I/Solution.sql @@ -1,10 +1,10 @@ # Write your MySQL query statement below SELECT - DATE_FORMAT(trans_date, '%Y-%m') AS month, + date_format(trans_date, '%Y-%m') AS month, country, - COUNT(*) AS trans_count, - COUNT(IF(state = 'approved', 1, NULL)) AS approved_count, - SUM(amount) AS trans_total_amount, - SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount + count(1) AS trans_count, + sum(state = 'approved') AS approved_count, + sum(amount) AS trans_total_amount, + sum(if(state = 'approved', amount, 0)) AS approved_total_amount FROM Transactions -GROUP BY month, country; +GROUP BY 1, 2; diff --git a/solution/1200-1299/1204.Last Person to Fit in the Bus/README.md b/solution/1200-1299/1204.Last Person to Fit in the Bus/README.md index d9c0e3bd21b7c..5edfe87f7f61c 100644 --- a/solution/1200-1299/1204.Last Person to Fit in the Bus/README.md +++ b/solution/1200-1299/1204.Last Person to Fit in the Bus/README.md @@ -81,4 +81,20 @@ ORDER BY a.turn DESC LIMIT 1; ``` +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT + person_name, + sum(weight) OVER (ORDER BY turn) AS s + FROM Queue + ) +SELECT person_name +FROM T +WHERE s <= 1000 +ORDER BY s DESC +LIMIT 1; +``` + diff --git a/solution/1200-1299/1204.Last Person to Fit in the Bus/README_EN.md b/solution/1200-1299/1204.Last Person to Fit in the Bus/README_EN.md index 0a1990e736dfe..0dfacf1d55409 100644 --- a/solution/1200-1299/1204.Last Person to Fit in the Bus/README_EN.md +++ b/solution/1200-1299/1204.Last Person to Fit in the Bus/README_EN.md @@ -84,4 +84,20 @@ ORDER BY a.turn DESC LIMIT 1; ``` +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT + person_name, + sum(weight) OVER (ORDER BY turn) AS s + FROM Queue + ) +SELECT person_name +FROM T +WHERE s <= 1000 +ORDER BY s DESC +LIMIT 1; +``` + diff --git a/solution/1200-1299/1204.Last Person to Fit in the Bus/Solution.sql b/solution/1200-1299/1204.Last Person to Fit in the Bus/Solution.sql index 1fe9b1d6334d5..1fc50f98a556f 100644 --- a/solution/1200-1299/1204.Last Person to Fit in the Bus/Solution.sql +++ b/solution/1200-1299/1204.Last Person to Fit in the Bus/Solution.sql @@ -1,10 +1,13 @@ # Write your MySQL query statement below -SELECT a.person_name -FROM - Queue AS a, - Queue AS b -WHERE a.turn >= b.turn -GROUP BY a.person_id -HAVING SUM(b.weight) <= 1000 -ORDER BY a.turn DESC +WITH + T AS ( + SELECT + person_name, + sum(weight) OVER (ORDER BY turn) AS s + FROM Queue + ) +SELECT person_name +FROM T +WHERE s <= 1000 +ORDER BY s DESC LIMIT 1; diff --git a/solution/1200-1299/1205.Monthly Transactions II/README.md b/solution/1200-1299/1205.Monthly Transactions II/README.md index 3efd486c7ec62..8f4f191f7fc3d 100644 --- a/solution/1200-1299/1205.Monthly Transactions II/README.md +++ b/solution/1200-1299/1205.Monthly Transactions II/README.md @@ -89,13 +89,13 @@ Chargebacks 表: ```sql # Write your MySQL query statement below WITH - t AS ( + T AS ( SELECT * FROM Transactions - UNION ALL - SELECT id, country, 'chargeback' AS state, amount, cb.trans_date + UNION + SELECT id, country, 'chargeback', amount, c.trans_date FROM - Chargebacks AS cb - LEFT JOIN Transactions AS tx ON cb.trans_id = tx.id + Transactions AS t + JOIN Chargebacks AS c ON t.id = c.trans_id ) SELECT date_format(trans_date, '%Y-%m') AS month, @@ -104,7 +104,7 @@ SELECT sum(if(state = 'approved', amount, 0)) AS approved_amount, sum(state = 'chargeback') AS chargeback_count, sum(if(state = 'chargeback', amount, 0)) AS chargeback_amount -FROM t +FROM T GROUP BY 1, 2 HAVING approved_amount OR chargeback_amount; ``` diff --git a/solution/1200-1299/1205.Monthly Transactions II/README_EN.md b/solution/1200-1299/1205.Monthly Transactions II/README_EN.md index 3b1417fde10f3..0cee8dda15313 100644 --- a/solution/1200-1299/1205.Monthly Transactions II/README_EN.md +++ b/solution/1200-1299/1205.Monthly Transactions II/README_EN.md @@ -86,13 +86,13 @@ Chargebacks table: ```sql # Write your MySQL query statement below WITH - t AS ( + T AS ( SELECT * FROM Transactions - UNION ALL - SELECT id, country, 'chargeback' AS state, amount, cb.trans_date + UNION + SELECT id, country, 'chargeback', amount, c.trans_date FROM - Chargebacks AS cb - LEFT JOIN Transactions AS tx ON cb.trans_id = tx.id + Transactions AS t + JOIN Chargebacks AS c ON t.id = c.trans_id ) SELECT date_format(trans_date, '%Y-%m') AS month, @@ -101,7 +101,7 @@ SELECT sum(if(state = 'approved', amount, 0)) AS approved_amount, sum(state = 'chargeback') AS chargeback_count, sum(if(state = 'chargeback', amount, 0)) AS chargeback_amount -FROM t +FROM T GROUP BY 1, 2 HAVING approved_amount OR chargeback_amount; ``` diff --git a/solution/1200-1299/1205.Monthly Transactions II/Solution.sql b/solution/1200-1299/1205.Monthly Transactions II/Solution.sql index 242ac44450f79..04059a380885c 100644 --- a/solution/1200-1299/1205.Monthly Transactions II/Solution.sql +++ b/solution/1200-1299/1205.Monthly Transactions II/Solution.sql @@ -1,12 +1,12 @@ # Write your MySQL query statement below WITH - t AS ( + T AS ( SELECT * FROM Transactions - UNION ALL - SELECT id, country, 'chargeback' AS state, amount, cb.trans_date + UNION + SELECT id, country, 'chargeback', amount, c.trans_date FROM - Chargebacks AS cb - LEFT JOIN Transactions AS tx ON cb.trans_id = tx.id + Transactions AS t + JOIN Chargebacks AS c ON t.id = c.trans_id ) SELECT date_format(trans_date, '%Y-%m') AS month, @@ -15,6 +15,6 @@ SELECT sum(if(state = 'approved', amount, 0)) AS approved_amount, sum(state = 'chargeback') AS chargeback_count, sum(if(state = 'chargeback', amount, 0)) AS chargeback_amount -FROM t +FROM T GROUP BY 1, 2 HAVING approved_amount OR chargeback_amount; diff --git a/solution/2700-2799/2772.Apply Operations to Make All Array Elements Equal to Zero/README_EN.md b/solution/2700-2799/2772.Apply Operations to Make All Array Elements Equal to Zero/README_EN.md index f816d205ab87b..7295d1529bb46 100644 --- a/solution/2700-2799/2772.Apply Operations to Make All Array Elements Equal to Zero/README_EN.md +++ b/solution/2700-2799/2772.Apply Operations to Make All Array Elements Equal to Zero/README_EN.md @@ -48,22 +48,22 @@ **Solution 1: Difference Array + Prefix Sum** -Let's first consider the first element $nums[0]$ of $nums$: +First, let's consider the first element of $nums$, $nums[0]$: -- If $nums[0] = 0$, then we don't need to do anything; -- If $nums[0] \gt 0$, then we need to operate $nums[0]$ times on $nums[0..k-1]$ to make the elements in $nums[0..k-1]$ all subtract $nums[0]$, so that $nums[0]$ becomes $0$. +- If $nums[0] = 0$, we don't need to do anything. +- If $nums[0] > 0$, we need to operate on $nums[0..k-1]$ for $nums[0]$ times, subtracting $nums[0]$ from all elements in $nums[0..k-1]$, so $nums[0]$ becomes $0$. -We can use difference array to maintain the operations on a segment of continuous elements. We use $d[i]$ to represent the difference array, and take the prefix sum of the difference array to get the variation amount of each position. +To perform the add and subtract operations on a contiguous segment of elements simultaneously, we can use a difference array to manage these operations. We represent the difference array with $d[i]$, and calculating the prefix sum of the difference array gives us the change of the value at each position. -Therefore, we traverse $nums$, for each element $nums[i]$, the current variation amount $s = \sum_{j=0}^{i} d[j]$ and we add $s$ to $nums[i]$ to get the actual value of $nums[i]$. +Therefore, we iterate over $nums$. For each element $nums[i]$, the current position's change quantity is $s = \sum_{j=0}^{i} d[j]$. We add $s$ to $nums[i]$ to get the actual value of $nums[i]$. -- If $nums[i] = 0$, then we don't need to do anything, just skip. -- If $nums[i]=0$ or $i + k \gt n$, it means that after the previous operation, $nums[i]$ has become negative, or $nums[i..i+k-1]$ out of bounds, so it is impossible to make all elements in $nums$ equal to $0$, then return `false`. Otherwise, we need to subtract $nums[i]$ from all elements in $[i..i+k-1]$, so we subtract $nums[i]$ from $s$ and add $nums[i]$ to $d[i+k]$. -- Continue to traverse the next element. +- If $nums[i] = 0$, there's no need for any operation, and we can skip directly. +- If $nums[i]=0$ or $i + k > n$, it indicates that after the previous operations, $nums[i]$ has become negative, or $nums[i..i+k-1]$ is out of bounds. Therefore, it's impossible to make all elements in $nums$ equal to $0$. We return `false`. Otherwise, we need to subtract $nums[i]$ from all elements in the interval $[i..i+k-1]$. Therefore, we subtract $nums[i]$ from $s$ and add $nums[i]$ to $d[i+k]$. +- We continue to iterate over the next element. -If the traversal is over, it means that we can make all elements in $nums$ equal to $0$, return `true`. +If the iteration ends, it means that all elements in $nums$ can be made equal to $0$, so we return `true`. -Time complexity $O(n)$, space complexity $O(n)$. Where $n$ is the length of the array $nums$. +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.