Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ SELECT
END
) AS Dec_Revenue
FROM Department
GROUP BY id;
GROUP BY 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ SELECT
END
) AS Dec_Revenue
FROM Department
GROUP BY id;
GROUP BY 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ SELECT
END
) AS Dec_Revenue
FROM Department
GROUP BY id;
GROUP BY 1;
16 changes: 8 additions & 8 deletions solution/1100-1199/1193.Monthly Transactions I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ Transactions</code> 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;
```

<!-- tabs:end -->
16 changes: 8 additions & 8 deletions solution/1100-1199/1193.Monthly Transactions I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```

<!-- tabs:end -->
12 changes: 6 additions & 6 deletions solution/1100-1199/1193.Monthly Transactions I/Solution.sql
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions solution/1200-1299/1204.Last Person to Fit in the Bus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```

<!-- tabs:end -->
16 changes: 16 additions & 0 deletions solution/1200-1299/1204.Last Person to Fit in the Bus/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```

<!-- tabs:end -->
19 changes: 11 additions & 8 deletions solution/1200-1299/1204.Last Person to Fit in the Bus/Solution.sql
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 6 additions & 6 deletions solution/1200-1299/1205.Monthly Transactions II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
```
Expand Down
12 changes: 6 additions & 6 deletions solution/1200-1299/1205.Monthly Transactions II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
```
Expand Down
12 changes: 6 additions & 6 deletions solution/1200-1299/1205.Monthly Transactions II/Solution.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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$.

<!-- tabs:start -->

Expand Down