Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d3af566

Browse files
feat: add solutions to lc problem: No.3214 (#3234)
No.3214.Year on Year Growth Rate
1 parent b1292ee commit d3af566

File tree

9 files changed

+360
-4
lines changed

9 files changed

+360
-4
lines changed

‎lcof2/剑指 Offer II 070. 排序数组中只出现一次的数字/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class Solution {
158158
func singleNonDuplicate(_ nums: [Int]) -> Int {
159159
var left = 0
160160
var right = nums.count - 1
161-
161+
162162
while left < right {
163163
let mid = (left + right) / 2
164164
if nums[mid] != nums[mid ^ 1] {
@@ -167,7 +167,7 @@ class Solution {
167167
left = mid + 1
168168
}
169169
}
170-
170+
171171
return nums[left]
172172
}
173173
}

‎solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ impl Solution {
33
if n == 1 {
44
return 1;
55
}
6-
let mut ans = (k + Solution::find_the_winner(n - 1, k)) % n;
7-
return if ans == 0 { n } else { ans };
6+
let mut ans = (k + Solution::find_the_winner(n - 1, k)) % n;
7+
return if ans == 0 { n } else { ans };
88
}
99
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
comments: true
3+
difficulty: 困难
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md
5+
tags:
6+
- 数据库
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [3214. Year on Year Growth Rate 🔒](https://leetcode.cn/problems/year-on-year-growth-rate)
12+
13+
[English Version](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md)
14+
15+
## 题目描述
16+
17+
<!-- description:start -->
18+
19+
<p>Table: <code>user_transactions</code></p>
20+
21+
<pre>
22+
+------------------+----------+
23+
| Column Name | Type |
24+
+------------------+----------+
25+
| transaction_id | integer |
26+
| product_id | integer |
27+
| spend | decimal |
28+
| transaction_date | datetime |
29+
+------------------+----------+
30+
The transaction_id column uniquely identifies each row in this table.
31+
Each row of this table contains the transaction ID, product ID, the spend amount, and the transaction date.
32+
</pre>
33+
34+
<p>Write a solution to calculate the <strong>year-on-year growth rate</strong> for the total spend <strong>for each product</strong>.</p>
35+
36+
<p>The result table should include the following columns:</p>
37+
38+
<ul>
39+
<li><code>year</code>: The year of the transaction.</li>
40+
<li><code>product_id</code>: The ID of the product.</li>
41+
<li><code>curr_year_spend</code>: The total spend for the current year.</li>
42+
<li><code>prev_year_spend</code>: The total spend for the previous year.</li>
43+
<li><code>yoy_rate</code>: The year-on-year growth rate percentage, rounded to <code>2</code> decimal places.</li>
44+
</ul>
45+
46+
<p>Return <em>the result table ordered by</em>&nbsp;<code>product_id</code>,<code>year</code> <em>in <strong>ascending</strong> order</em>.</p>
47+
48+
<p>The result format is in the following example.</p>
49+
50+
<p>&nbsp;</p>
51+
<p><strong class="example">Example:</strong></p>
52+
53+
<div class="example-block">
54+
<p><strong>Input:</strong></p>
55+
56+
<p><code>user_transactions</code> table:</p>
57+
58+
<pre class="example-io">
59+
+----------------+------------+---------+---------------------+
60+
| transaction_id | product_id | spend | transaction_date |
61+
+----------------+------------+---------+---------------------+
62+
| 1341 | 123424 | 1500.60 | 2019年12月31日 12:00:00 |
63+
| 1423 | 123424 | 1000.20 | 2020年12月31日 12:00:00 |
64+
| 1623 | 123424 | 1246.44 | 2021年12月31日 12:00:00 |
65+
| 1322 | 123424 | 2145.32 | 2022年12月31日 12:00:00 |
66+
+----------------+------------+---------+---------------------+
67+
</pre>
68+
69+
<p><strong>Output:</strong></p>
70+
71+
<pre class="example-io">
72+
+------+------------+----------------+----------------+----------+
73+
| year | product_id | curr_year_spend| prev_year_spend| yoy_rate |
74+
+------+------------+----------------+----------------+----------+
75+
| 2019 | 123424 | 1500.60 | NULL | NULL |
76+
| 2020 | 123424 | 1000.20 | 1500.60 | -33.35 |
77+
| 2021 | 123424 | 1246.44 | 1000.20 | 24.62 |
78+
| 2022 | 123424 | 2145.32 | 1246.44 | 72.12 |
79+
+------+------------+----------------+----------------+----------+
80+
</pre>
81+
82+
<p><strong>Explanation:</strong></p>
83+
84+
<ul>
85+
<li>For product ID 123424:
86+
<ul>
87+
<li>In 2019:
88+
<ul>
89+
<li>Current year&#39;s spend is 1500.60</li>
90+
<li>No previous year&#39;s spend recorded</li>
91+
<li>YoY growth rate: NULL</li>
92+
</ul>
93+
</li>
94+
<li>In 2020:
95+
<ul>
96+
<li>Current year&#39;s spend is 1000.20</li>
97+
<li>Previous year&#39;s spend is 1500.60</li>
98+
<li>YoY growth rate: ((1000.20 - 1500.60) / 1500.60) * 100 = -33.35%</li>
99+
</ul>
100+
</li>
101+
<li>In 2021:
102+
<ul>
103+
<li>Current year&#39;s spend is 1246.44</li>
104+
<li>Previous year&#39;s spend is 1000.20</li>
105+
<li>YoY growth rate: ((1246.44 - 1000.20) / 1000.20) * 100 = 24.62%</li>
106+
</ul>
107+
</li>
108+
<li>In 2022:
109+
<ul>
110+
<li>Current year&#39;s spend is 2145.32</li>
111+
<li>Previous year&#39;s spend is 1246.44</li>
112+
<li>YoY growth rate: ((2145.32 - 1246.44) / 1246.44) * 100 = 72.12%</li>
113+
</ul>
114+
</li>
115+
</ul>
116+
</li>
117+
</ul>
118+
119+
<p><strong>Note:</strong> Output table is ordered by <code>product_id</code> and <code>year</code> in ascending order.</p>
120+
</div>
121+
122+
<!-- description:end -->
123+
124+
## 解法
125+
126+
<!-- solution:start -->
127+
128+
### 方法一:分组统计 + 窗口函数
129+
130+
我们可以先按照年份和产品 ID 进行分组统计每个产品每年的总花费,记录在 `T` 表中。然后使用窗口函数 `LAG` 计算出上一年的总花费,记录在 `S` 表中。最后根据公式计算出年增长率。
131+
132+
<!-- tabs:start -->
133+
134+
#### MySQL
135+
136+
```sql
137+
# Write your MySQL query statement below
138+
WITH
139+
T AS (
140+
SELECT YEAR(transaction_date) year, product_id, SUM(spend) tot_spend
141+
FROM user_transactions
142+
GROUP BY 1, 2
143+
),
144+
S AS (
145+
SELECT
146+
year,
147+
product_id,
148+
tot_spend curr_year_spend,
149+
LAG(tot_spend) OVER (
150+
PARTITION BY product_id
151+
ORDER BY year
152+
) prev_year_spend
153+
FROM T
154+
)
155+
SELECT
156+
*,
157+
ROUND((curr_year_spend - prev_year_spend) / prev_year_spend * 100, 2) yoy_rate
158+
FROM S;
159+
```
160+
161+
<!-- tabs:end -->
162+
163+
<!-- solution:end -->
164+
165+
<!-- problem:end -->
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
comments: true
3+
difficulty: Hard
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md
5+
tags:
6+
- Database
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [3214. Year on Year Growth Rate 🔒](https://leetcode.com/problems/year-on-year-growth-rate)
12+
13+
[中文文档](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md)
14+
15+
## Description
16+
17+
<!-- description:start -->
18+
19+
<p>Table: <code>user_transactions</code></p>
20+
21+
<pre>
22+
+------------------+----------+
23+
| Column Name | Type |
24+
+------------------+----------+
25+
| transaction_id | integer |
26+
| product_id | integer |
27+
| spend | decimal |
28+
| transaction_date | datetime |
29+
+------------------+----------+
30+
The transaction_id column uniquely identifies each row in this table.
31+
Each row of this table contains the transaction ID, product ID, the spend amount, and the transaction date.
32+
</pre>
33+
34+
<p>Write a solution to calculate the <strong>year-on-year growth rate</strong> for the total spend <strong>for each product</strong>.</p>
35+
36+
<p>The result table should include the following columns:</p>
37+
38+
<ul>
39+
<li><code>year</code>: The year of the transaction.</li>
40+
<li><code>product_id</code>: The ID of the product.</li>
41+
<li><code>curr_year_spend</code>: The total spend for the current year.</li>
42+
<li><code>prev_year_spend</code>: The total spend for the previous year.</li>
43+
<li><code>yoy_rate</code>: The year-on-year growth rate percentage, rounded to <code>2</code> decimal places.</li>
44+
</ul>
45+
46+
<p>Return <em>the result table ordered by</em>&nbsp;<code>product_id</code>,<code>year</code> <em>in <strong>ascending</strong> order</em>.</p>
47+
48+
<p>The result format is in the following example.</p>
49+
50+
<p>&nbsp;</p>
51+
<p><strong class="example">Example:</strong></p>
52+
53+
<div class="example-block">
54+
<p><strong>Input:</strong></p>
55+
56+
<p><code>user_transactions</code> table:</p>
57+
58+
<pre class="example-io">
59+
+----------------+------------+---------+---------------------+
60+
| transaction_id | product_id | spend | transaction_date |
61+
+----------------+------------+---------+---------------------+
62+
| 1341 | 123424 | 1500.60 | 2019年12月31日 12:00:00 |
63+
| 1423 | 123424 | 1000.20 | 2020年12月31日 12:00:00 |
64+
| 1623 | 123424 | 1246.44 | 2021年12月31日 12:00:00 |
65+
| 1322 | 123424 | 2145.32 | 2022年12月31日 12:00:00 |
66+
+----------------+------------+---------+---------------------+
67+
</pre>
68+
69+
<p><strong>Output:</strong></p>
70+
71+
<pre class="example-io">
72+
+------+------------+----------------+----------------+----------+
73+
| year | product_id | curr_year_spend| prev_year_spend| yoy_rate |
74+
+------+------------+----------------+----------------+----------+
75+
| 2019 | 123424 | 1500.60 | NULL | NULL |
76+
| 2020 | 123424 | 1000.20 | 1500.60 | -33.35 |
77+
| 2021 | 123424 | 1246.44 | 1000.20 | 24.62 |
78+
| 2022 | 123424 | 2145.32 | 1246.44 | 72.12 |
79+
+------+------------+----------------+----------------+----------+
80+
</pre>
81+
82+
<p><strong>Explanation:</strong></p>
83+
84+
<ul>
85+
<li>For product ID 123424:
86+
<ul>
87+
<li>In 2019:
88+
<ul>
89+
<li>Current year&#39;s spend is 1500.60</li>
90+
<li>No previous year&#39;s spend recorded</li>
91+
<li>YoY growth rate: NULL</li>
92+
</ul>
93+
</li>
94+
<li>In 2020:
95+
<ul>
96+
<li>Current year&#39;s spend is 1000.20</li>
97+
<li>Previous year&#39;s spend is 1500.60</li>
98+
<li>YoY growth rate: ((1000.20 - 1500.60) / 1500.60) * 100 = -33.35%</li>
99+
</ul>
100+
</li>
101+
<li>In 2021:
102+
<ul>
103+
<li>Current year&#39;s spend is 1246.44</li>
104+
<li>Previous year&#39;s spend is 1000.20</li>
105+
<li>YoY growth rate: ((1246.44 - 1000.20) / 1000.20) * 100 = 24.62%</li>
106+
</ul>
107+
</li>
108+
<li>In 2022:
109+
<ul>
110+
<li>Current year&#39;s spend is 2145.32</li>
111+
<li>Previous year&#39;s spend is 1246.44</li>
112+
<li>YoY growth rate: ((2145.32 - 1246.44) / 1246.44) * 100 = 72.12%</li>
113+
</ul>
114+
</li>
115+
</ul>
116+
</li>
117+
</ul>
118+
119+
<p><strong>Note:</strong> Output table is ordered by <code>product_id</code> and <code>year</code> in ascending order.</p>
120+
</div>
121+
122+
<!-- description:end -->
123+
124+
## Solutions
125+
126+
<!-- solution:start -->
127+
128+
### Solution 1: Grouping Statistics + Window Function
129+
130+
We can first group by year and product ID to calculate the total cost of each product every year, recorded in table `T`. Then, use the window function `LAG` to calculate the total cost of the previous year, recorded in table `S`. Finally, calculate the annual growth rate based on the formula.
131+
132+
<!-- tabs:start -->
133+
134+
#### MySQL
135+
136+
```sql
137+
# Write your MySQL query statement below
138+
WITH
139+
T AS (
140+
SELECT YEAR(transaction_date) year, product_id, SUM(spend) tot_spend
141+
FROM user_transactions
142+
GROUP BY 1, 2
143+
),
144+
S AS (
145+
SELECT
146+
year,
147+
product_id,
148+
tot_spend curr_year_spend,
149+
LAG(tot_spend) OVER (
150+
PARTITION BY product_id
151+
ORDER BY year
152+
) prev_year_spend
153+
FROM T
154+
)
155+
SELECT
156+
*,
157+
ROUND((curr_year_spend - prev_year_spend) / prev_year_spend * 100, 2) yoy_rate
158+
FROM S;
159+
```
160+
161+
<!-- tabs:end -->
162+
163+
<!-- solution:end -->
164+
165+
<!-- problem:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT YEAR(transaction_date) year, product_id, SUM(spend) tot_spend
5+
FROM user_transactions
6+
GROUP BY 1, 2
7+
),
8+
S AS (
9+
SELECT
10+
year,
11+
product_id,
12+
tot_spend curr_year_spend,
13+
LAG(tot_spend) OVER (
14+
PARTITION BY product_id
15+
ORDER BY year
16+
) prev_year_spend
17+
FROM T
18+
)
19+
SELECT
20+
*,
21+
ROUND((curr_year_spend - prev_year_spend) / prev_year_spend * 100, 2) yoy_rate
22+
FROM S;

‎solution/DATABASE_README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@
286286
| 3188 | [查找得分最高的学生 II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | `数据库` | 困难 | 🔒 |
287287
| 3198 | [查找每个州的城市](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md) | `数据库` | 简单 | 🔒 |
288288
| 3204 | [按位用户权限分析](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README.md) | `数据库` | 中等 | 🔒 |
289+
| 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md) | `数据库` | 困难 | 🔒 |
289290

290291
## 版权
291292

‎solution/DATABASE_README_EN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
284284
| 3188 | [Find Top Scoring Students II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README_EN.md) | `Database` | Hard | 🔒 |
285285
| 3198 | [Find Cities in Each State](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md) | `Database` | Easy | 🔒 |
286286
| 3204 | [Bitwise User Permissions Analysis](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README_EN.md) | `Database` | Medium | 🔒 |
287+
| 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md) | `Database` | Hard | 🔒 |
287288

288289
## Copyright
289290

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /