Skip to content
/ leetcode Public
  • Sponsor doocs/leetcode

  • Notifications You must be signed in to change notification settings
  • Fork 9.1k

Commit 9c63941

Browse files
authoredJul 9, 2024··
feat: update lc problems (#3235)
1 parent d3af566 commit 9c63941

File tree

24 files changed

+150
-93
lines changed

24 files changed

+150
-93
lines changed
 

‎solution/3200-3299/3205.Maximum Array Hopping Score I/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3205.Maximum%20Array%20Hopping%20Score%20I/README.md
5+
tags:
6+
-
7+
- 数组
8+
- 动态规划
9+
- 单调栈
510
---
611

712
<!-- problem:start -->

‎solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3205.Maximum%20Array%20Hopping%20Score%20I/README_EN.md
5+
tags:
6+
- Stack
7+
- Array
8+
- Dynamic Programming
9+
- Monotonic Stack
510
---
611

712
<!-- problem:start -->

‎solution/3200-3299/3206.Alternating Groups I/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: 简单
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3206.Alternating%20Groups%20I/README.md
5+
tags:
6+
- 数组
7+
- 滑动窗口
58
---
69

710
<!-- problem:start -->

‎solution/3200-3299/3206.Alternating Groups I/README_EN.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: Easy
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3206.Alternating%20Groups%20I/README_EN.md
5+
tags:
6+
- Array
7+
- Sliding Window
58
---
69

710
<!-- problem:start -->

‎solution/3200-3299/3207.Maximum Points After Enemy Battles/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3207.Maximum%20Points%20After%20Enemy%20Battles/README.md
5+
tags:
6+
- 贪心
7+
- 数组
58
---
69

710
<!-- problem:start -->

‎solution/3200-3299/3207.Maximum Points After Enemy Battles/README_EN.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3207.Maximum%20Points%20After%20Enemy%20Battles/README_EN.md
5+
tags:
6+
- Greedy
7+
- Array
58
---
69

710
<!-- problem:start -->

‎solution/3200-3299/3208.Alternating Groups II/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3208.Alternating%20Groups%20II/README.md
5+
tags:
6+
- 数组
7+
- 滑动窗口
58
---
69

710
<!-- problem:start -->

‎solution/3200-3299/3208.Alternating Groups II/README_EN.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3208.Alternating%20Groups%20II/README_EN.md
5+
tags:
6+
- Array
7+
- Sliding Window
58
---
69

710
<!-- problem:start -->

‎solution/3200-3299/3209.Number of Subarrays With AND Value of K/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3209.Number%20of%20Subarrays%20With%20AND%20Value%20of%20K/README.md
5+
tags:
6+
- 位运算
7+
- 线段树
8+
- 数组
9+
- 二分查找
510
---
611

712
<!-- problem:start -->

‎solution/3200-3299/3209.Number of Subarrays With AND Value of K/README_EN.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
comments: true
33
difficulty: Hard
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3209.Number%20of%20Subarrays%20With%20AND%20Value%20of%20K/README_EN.md
5+
tags:
6+
- Bit Manipulation
7+
- Segment Tree
8+
- Array
9+
- Binary Search
510
---
611

712
<!-- problem:start -->

‎solution/3200-3299/3210.Find the Encrypted String/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 简单
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3210.Find%20the%20Encrypted%20String/README.md
5+
tags:
6+
- 字符串
57
---
68

79
<!-- problem:start -->

‎solution/3200-3299/3210.Find the Encrypted String/README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Easy
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3210.Find%20the%20Encrypted%20String/README_EN.md
5+
tags:
6+
- String
57
---
68

79
<!-- problem:start -->

‎solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3211.Generate%20Binary%20Strings%20Without%20Adjacent%20Zeros/README.md
5+
tags:
6+
- 位运算
7+
- 递归
8+
- 字符串
59
---
610

711
<!-- problem:start -->

‎solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3211.Generate%20Binary%20Strings%20Without%20Adjacent%20Zeros/README_EN.md
5+
tags:
6+
- Bit Manipulation
7+
- Recursion
8+
- String
59
---
610

711
<!-- problem:start -->

‎solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3212.Count%20Submatrices%20With%20Equal%20Frequency%20of%20X%20and%20Y/README.md
5+
tags:
6+
- 数组
7+
- 矩阵
8+
- 前缀和
59
---
610

711
<!-- problem:start -->

‎solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3212.Count%20Submatrices%20With%20Equal%20Frequency%20of%20X%20and%20Y/README_EN.md
5+
tags:
6+
- Array
7+
- Matrix
8+
- Prefix Sum
59
---
610

711
<!-- problem:start -->

‎solution/3200-3299/3213.Construct String with Minimum Cost/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3213.Construct%20String%20with%20Minimum%20Cost/README.md
5+
tags:
6+
- 数组
7+
- 字符串
8+
- 动态规划
9+
- 后缀数组
510
---
611

712
<!-- problem:start -->

‎solution/3200-3299/3213.Construct String with Minimum Cost/README_EN.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
comments: true
33
difficulty: Hard
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3213.Construct%20String%20with%20Minimum%20Cost/README_EN.md
5+
tags:
6+
- Array
7+
- String
8+
- Dynamic Programming
9+
- Suffix Array
510
---
611

712
<!-- problem:start -->

‎solution/3200-3299/3214.Year on Year Growth Rate/README.md

+46-49
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ tags:
88

99
<!-- problem:start -->
1010

11-
# [3214. Year on Year Growth Rate 🔒](https://leetcode.cn/problems/year-on-year-growth-rate)
11+
# [3214. 同比增长率 🔒](https://leetcode.cn/problems/year-on-year-growth-rate)
1212

1313
[English Version](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md)
1414

1515
## 题目描述
1616

1717
<!-- description:start -->
1818

19-
<p>Table: <code>user_transactions</code></p>
19+
<p>表:<code>user_transactions</code></p>
2020

2121
<pre>
2222
+------------------+----------+
@@ -27,33 +27,34 @@ tags:
2727
| spend | decimal |
2828
| transaction_date | datetime |
2929
+------------------+----------+
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.
30+
transaction_id 列唯一标识了表中的每一列。
31+
这张表的每一行含有交易 ID,产品 ID,总花费以及交易日期。
3232
</pre>
3333

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>
34+
<p>编写一个解决方案来计算 <strong>每个产品</strong> 总支出的 <strong>同比增长率</strong></p>
3535

36-
<p>The result table should include the following columns:</p>
36+
<p>结果表应该包含以下列:</p>
3737

3838
<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>
39+
<li><code>year</code>:交易的年份。</li>
40+
<li><code>product_id</code>:产品的 ID。</li>
41+
<li><code>curr_year_spend</code>:当年的总支出。</li>
42+
<li><code>prev_year_spend</code>:上一年的总支出。</li>
43+
<li><code>yoy_rate</code>:同比增速百分比,四舍五入至小数点后 2 位。</li>
4444
</ul>
4545

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>
46+
<p>返回结果表以&nbsp;<code>product_id</code><code>year</code>&nbsp;<strong>升序</strong> 排序。</p>
4747

48-
<p>The result format is in the following example.</p>
48+
<p>结果格式如下所示。</p>
4949

5050
<p>&nbsp;</p>
51-
<p><strong class="example">Example:</strong></p>
51+
52+
<p><strong class="example">示例:</strong></p>
5253

5354
<div class="example-block">
54-
<p><strong>Input:</strong></p>
55+
<p><strong>输入:</strong></p>
5556

56-
<p><code>user_transactions</code> table:</p>
57+
<p><code>user_transactions</code> 表:</p>
5758

5859
<pre class="example-io">
5960
+----------------+------------+---------+---------------------+
@@ -66,7 +67,7 @@ Each row of this table contains the transaction ID, product ID, the spend amount
6667
+----------------+------------+---------+---------------------+
6768
</pre>
6869

69-
<p><strong>Output:</strong></p>
70+
<p><strong>输出:</strong></p>
7071

7172
<pre class="example-io">
7273
+------+------------+----------------+----------------+----------+
@@ -79,44 +80,44 @@ Each row of this table contains the transaction ID, product ID, the spend amount
7980
+------+------------+----------------+----------------+----------+
8081
</pre>
8182

82-
<p><strong>Explanation:</strong></p>
83+
<p><strong>解释:</strong></p>
8384

8485
<ul>
85-
<li>For product ID 123424:
86+
<li>对于产品 ID 123424:
8687
<ul>
87-
<li>In 2019:
88+
<li> 2019
8889
<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>
90+
<li>当年的支出是 1500.60</li>
91+
<li>没有上一年支出的记录</li>
92+
<li>同比增长率:NULL</li>
9293
</ul>
9394
</li>
94-
<li>In 2020:
95+
<li> 2020
9596
<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>
97+
<li>当年的支出是 1000.20</li>
98+
<li>上一年的支出是 1500.60</li>
99+
<li>同比增长率:((1000.20 - 1500.60) / 1500.60) * 100 = -33.35%</li>
99100
</ul>
100101
</li>
101-
<li>In 2021:
102+
<li> 2021
102103
<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>
104+
<li>当年的支出是 1246.44</li>
105+
<li>上一年的支出是 1000.20</li>
106+
<li>同比增长率:((1246.44 - 1000.20) / 1000.20) * 100 = 24.62%</li>
106107
</ul>
107108
</li>
108-
<li>In 2022:
109+
<li> 2022
109110
<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>
111+
<li>当年的支出是 2145.32</li>
112+
<li>上一年的支出是 1246.44</li>
113+
<li>同比增长率:((2145.32 - 1246.44) / 1246.44) * 100 = 72.12%</li>
113114
</ul>
114115
</li>
115116
</ul>
116117
</li>
117118
</ul>
118119

119-
<p><strong>Note:</strong> Output table is ordered by <code>product_id</code> and <code>year</code> in ascending order.</p>
120+
<p><strong>注意:</strong>输出表以&nbsp;<code>product_id</code> &nbsp;<code>year</code>&nbsp;升序排序。</p>
120121
</div>
121122

122123
<!-- description:end -->
@@ -125,9 +126,9 @@ Each row of this table contains the transaction ID, product ID, the spend amount
125126

126127
<!-- solution:start -->
127128

128-
### 方法一:分组统计 + 窗口函数
129+
### 方法一:分组统计 + 左连接
129130

130-
我们可以先按照年份和产品 ID 进行分组统计每个产品每年的总花费,记录在 `T` 表中。然后使用窗口函数 `LAG` 计算出上一年的总花费,记录在 `S` 表中。最后根据公式计算出年增长率
131+
我们可以先按照 `product_id``year(transaction_date)` 进行分组统计,然后使用左连接将当前年份的统计结果与上一年份的统计结果进行关联,最后计算年同比增长率
131132

132133
<!-- tabs:start -->
133134

@@ -137,25 +138,21 @@ Each row of this table contains the transaction ID, product ID, the spend amount
137138
# Write your MySQL query statement below
138139
WITH
139140
T AS (
140-
SELECT YEAR(transaction_date) year, product_id, SUM(spend) tot_spend
141+
SELECT product_id, YEAR(transaction_date) year, SUM(spend) curr_year_spend
141142
FROM user_transactions
142143
GROUP BY 1, 2
143144
),
144145
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
146+
SELECT t1.year, t1.product_id, t1.curr_year_spend, t2.curr_year_spend prev_year_spend
147+
FROM
148+
T t1
149+
LEFT JOIN T t2 ON t1.product_id = t2.product_id AND t1.year = t2.year + 1
154150
)
155151
SELECT
156152
*,
157153
ROUND((curr_year_spend - prev_year_spend) / prev_year_spend * 100, 2) yoy_rate
158-
FROM S;
154+
FROM S
155+
ORDER BY 2, 1;
159156
```
160157

161158
<!-- tabs:end -->

0 commit comments

Comments
 (0)
Please sign in to comment.