diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md"
index fc9c626d119c5..a7e4f21e54570 100644
--- "a/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md"
+++ "b/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md"
@@ -158,7 +158,7 @@ class Solution {
func singleNonDuplicate(_ nums: [Int]) -> Int {
var left = 0
var right = nums.count - 1
-
+
while left < right {
let mid = (left + right) / 2
if nums[mid] != nums[mid ^ 1] {
@@ -167,7 +167,7 @@ class Solution {
left = mid + 1
}
}
-
+
return nums[left]
}
}
diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.rs b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.rs
index 1e5c3c839521c..ea4fa35d71619 100644
--- a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.rs
+++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.rs
@@ -3,7 +3,7 @@ impl Solution {
if n == 1 {
return 1;
}
- let mut ans = (k + Solution::find_the_winner(n - 1, k)) % n;
- return if ans == 0 { n } else { ans };
+ let mut ans = (k + Solution::find_the_winner(n - 1, k)) % n;
+ return if ans == 0 { n } else { ans };
}
}
diff --git a/solution/3200-3299/3214.Year on Year Growth Rate/README.md b/solution/3200-3299/3214.Year on Year Growth Rate/README.md
new file mode 100644
index 0000000000000..6dc562371bf6b
--- /dev/null
+++ b/solution/3200-3299/3214.Year on Year Growth Rate/README.md
@@ -0,0 +1,165 @@
+---
+comments: true
+difficulty: 困难
+edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md
+tags:
+ - 数据库
+---
+
+
+
+# [3214. Year on Year Growth Rate 🔒](https://leetcode.cn/problems/year-on-year-growth-rate)
+
+[English Version](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md)
+
+## 题目描述
+
+
+
+
Table: user_transactions
+
+
++------------------+----------+
+| Column Name | Type |
++------------------+----------+
+| transaction_id | integer |
+| product_id | integer |
+| spend | decimal |
+| transaction_date | datetime |
++------------------+----------+
+The transaction_id column uniquely identifies each row in this table.
+Each row of this table contains the transaction ID, product ID, the spend amount, and the transaction date.
+
+
+Write a solution to calculate the year-on-year growth rate for the total spend for each product.
+
+The result table should include the following columns:
+
+
+ year
: The year of the transaction.
+ product_id
: The ID of the product.
+ curr_year_spend
: The total spend for the current year.
+ prev_year_spend
: The total spend for the previous year.
+ yoy_rate
: The year-on-year growth rate percentage, rounded to 2
decimal places.
+
+
+Return the result table ordered by product_id
,year
in ascending order.
+
+The result format is in the following example.
+
+
+Example:
+
+
+
Input:
+
+
user_transactions
table:
+
+
++----------------+------------+---------+---------------------+
+| transaction_id | product_id | spend | transaction_date |
++----------------+------------+---------+---------------------+
+| 1341 | 123424 | 1500.60 | 2019-12-31 12:00:00 |
+| 1423 | 123424 | 1000.20 | 2020-12-31 12:00:00 |
+| 1623 | 123424 | 1246.44 | 2021-12-31 12:00:00 |
+| 1322 | 123424 | 2145.32 | 2022-12-31 12:00:00 |
++----------------+------------+---------+---------------------+
+
+
+
Output:
+
+
++------+------------+----------------+----------------+----------+
+| year | product_id | curr_year_spend| prev_year_spend| yoy_rate |
++------+------------+----------------+----------------+----------+
+| 2019 | 123424 | 1500.60 | NULL | NULL |
+| 2020 | 123424 | 1000.20 | 1500.60 | -33.35 |
+| 2021 | 123424 | 1246.44 | 1000.20 | 24.62 |
+| 2022 | 123424 | 2145.32 | 1246.44 | 72.12 |
++------+------------+----------------+----------------+----------+
+
+
+
Explanation:
+
+
+ - For product ID 123424:
+
+ - In 2019:
+
+ - Current year's spend is 1500.60
+ - No previous year's spend recorded
+ - YoY growth rate: NULL
+
+
+ - In 2020:
+
+ - Current year's spend is 1000.20
+ - Previous year's spend is 1500.60
+ - YoY growth rate: ((1000.20 - 1500.60) / 1500.60) * 100 = -33.35%
+
+
+ - In 2021:
+
+ - Current year's spend is 1246.44
+ - Previous year's spend is 1000.20
+ - YoY growth rate: ((1246.44 - 1000.20) / 1000.20) * 100 = 24.62%
+
+
+ - In 2022:
+
+ - Current year's spend is 2145.32
+ - Previous year's spend is 1246.44
+ - YoY growth rate: ((2145.32 - 1246.44) / 1246.44) * 100 = 72.12%
+
+
+
+
+
+
+
Note: Output table is ordered by product_id
and year
in ascending order.
+
+
+
+
+## 解法
+
+
+
+### 方法一:分组统计 + 窗口函数
+
+我们可以先按照年份和产品 ID 进行分组统计每个产品每年的总花费,记录在 `T` 表中。然后使用窗口函数 `LAG` 计算出上一年的总花费,记录在 `S` 表中。最后根据公式计算出年增长率。
+
+
+
+#### MySQL
+
+```sql
+# Write your MySQL query statement below
+WITH
+ T AS (
+ SELECT YEAR(transaction_date) year, product_id, SUM(spend) tot_spend
+ FROM user_transactions
+ GROUP BY 1, 2
+ ),
+ S AS (
+ SELECT
+ year,
+ product_id,
+ tot_spend curr_year_spend,
+ LAG(tot_spend) OVER (
+ PARTITION BY product_id
+ ORDER BY year
+ ) prev_year_spend
+ FROM T
+ )
+SELECT
+ *,
+ ROUND((curr_year_spend - prev_year_spend) / prev_year_spend * 100, 2) yoy_rate
+FROM S;
+```
+
+
+
+
+
+
diff --git a/solution/3200-3299/3214.Year on Year Growth Rate/README_EN.md b/solution/3200-3299/3214.Year on Year Growth Rate/README_EN.md
new file mode 100644
index 0000000000000..8e86a0c886ed4
--- /dev/null
+++ b/solution/3200-3299/3214.Year on Year Growth Rate/README_EN.md
@@ -0,0 +1,165 @@
+---
+comments: true
+difficulty: Hard
+edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md
+tags:
+ - Database
+---
+
+
+
+# [3214. Year on Year Growth Rate 🔒](https://leetcode.com/problems/year-on-year-growth-rate)
+
+[中文文档](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md)
+
+## Description
+
+
+
+Table: user_transactions
+
+
++------------------+----------+
+| Column Name | Type |
++------------------+----------+
+| transaction_id | integer |
+| product_id | integer |
+| spend | decimal |
+| transaction_date | datetime |
++------------------+----------+
+The transaction_id column uniquely identifies each row in this table.
+Each row of this table contains the transaction ID, product ID, the spend amount, and the transaction date.
+
+
+Write a solution to calculate the year-on-year growth rate for the total spend for each product.
+
+The result table should include the following columns:
+
+
+ year
: The year of the transaction.
+ product_id
: The ID of the product.
+ curr_year_spend
: The total spend for the current year.
+ prev_year_spend
: The total spend for the previous year.
+ yoy_rate
: The year-on-year growth rate percentage, rounded to 2
decimal places.
+
+
+Return the result table ordered by product_id
,year
in ascending order.
+
+The result format is in the following example.
+
+
+Example:
+
+
+
Input:
+
+
user_transactions
table:
+
+
++----------------+------------+---------+---------------------+
+| transaction_id | product_id | spend | transaction_date |
++----------------+------------+---------+---------------------+
+| 1341 | 123424 | 1500.60 | 2019-12-31 12:00:00 |
+| 1423 | 123424 | 1000.20 | 2020-12-31 12:00:00 |
+| 1623 | 123424 | 1246.44 | 2021-12-31 12:00:00 |
+| 1322 | 123424 | 2145.32 | 2022-12-31 12:00:00 |
++----------------+------------+---------+---------------------+
+
+
+
Output:
+
+
++------+------------+----------------+----------------+----------+
+| year | product_id | curr_year_spend| prev_year_spend| yoy_rate |
++------+------------+----------------+----------------+----------+
+| 2019 | 123424 | 1500.60 | NULL | NULL |
+| 2020 | 123424 | 1000.20 | 1500.60 | -33.35 |
+| 2021 | 123424 | 1246.44 | 1000.20 | 24.62 |
+| 2022 | 123424 | 2145.32 | 1246.44 | 72.12 |
++------+------------+----------------+----------------+----------+
+
+
+
Explanation:
+
+
+ - For product ID 123424:
+
+ - In 2019:
+
+ - Current year's spend is 1500.60
+ - No previous year's spend recorded
+ - YoY growth rate: NULL
+
+
+ - In 2020:
+
+ - Current year's spend is 1000.20
+ - Previous year's spend is 1500.60
+ - YoY growth rate: ((1000.20 - 1500.60) / 1500.60) * 100 = -33.35%
+
+
+ - In 2021:
+
+ - Current year's spend is 1246.44
+ - Previous year's spend is 1000.20
+ - YoY growth rate: ((1246.44 - 1000.20) / 1000.20) * 100 = 24.62%
+
+
+ - In 2022:
+
+ - Current year's spend is 2145.32
+ - Previous year's spend is 1246.44
+ - YoY growth rate: ((2145.32 - 1246.44) / 1246.44) * 100 = 72.12%
+
+
+
+
+
+
+
Note: Output table is ordered by product_id
and year
in ascending order.
+
+
+
+
+## Solutions
+
+
+
+### Solution 1: Grouping Statistics + Window Function
+
+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.
+
+
+
+#### MySQL
+
+```sql
+# Write your MySQL query statement below
+WITH
+ T AS (
+ SELECT YEAR(transaction_date) year, product_id, SUM(spend) tot_spend
+ FROM user_transactions
+ GROUP BY 1, 2
+ ),
+ S AS (
+ SELECT
+ year,
+ product_id,
+ tot_spend curr_year_spend,
+ LAG(tot_spend) OVER (
+ PARTITION BY product_id
+ ORDER BY year
+ ) prev_year_spend
+ FROM T
+ )
+SELECT
+ *,
+ ROUND((curr_year_spend - prev_year_spend) / prev_year_spend * 100, 2) yoy_rate
+FROM S;
+```
+
+
+
+
+
+
diff --git a/solution/3200-3299/3214.Year on Year Growth Rate/Solution.sql b/solution/3200-3299/3214.Year on Year Growth Rate/Solution.sql
new file mode 100644
index 0000000000000..ebe59cb961f67
--- /dev/null
+++ b/solution/3200-3299/3214.Year on Year Growth Rate/Solution.sql
@@ -0,0 +1,22 @@
+# Write your MySQL query statement below
+WITH
+ T AS (
+ SELECT YEAR(transaction_date) year, product_id, SUM(spend) tot_spend
+ FROM user_transactions
+ GROUP BY 1, 2
+ ),
+ S AS (
+ SELECT
+ year,
+ product_id,
+ tot_spend curr_year_spend,
+ LAG(tot_spend) OVER (
+ PARTITION BY product_id
+ ORDER BY year
+ ) prev_year_spend
+ FROM T
+ )
+SELECT
+ *,
+ ROUND((curr_year_spend - prev_year_spend) / prev_year_spend * 100, 2) yoy_rate
+FROM S;
diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md
index 816a7fd10edf3..3109801c2af85 100644
--- a/solution/DATABASE_README.md
+++ b/solution/DATABASE_README.md
@@ -286,6 +286,7 @@
| 3188 | [查找得分最高的学生 II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | `数据库` | 困难 | 🔒 |
| 3198 | [查找每个州的城市](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md) | `数据库` | 简单 | 🔒 |
| 3204 | [按位用户权限分析](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README.md) | `数据库` | 中等 | 🔒 |
+| 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md) | `数据库` | 困难 | 🔒 |
## 版权
diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md
index 4b21c83adf29c..18d7f63a90c8e 100644
--- a/solution/DATABASE_README_EN.md
+++ b/solution/DATABASE_README_EN.md
@@ -284,6 +284,7 @@ Press Control + F(or Command + F on
| 3188 | [Find Top Scoring Students II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README_EN.md) | `Database` | Hard | 🔒 |
| 3198 | [Find Cities in Each State](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md) | `Database` | Easy | 🔒 |
| 3204 | [Bitwise User Permissions Analysis](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README_EN.md) | `Database` | Medium | 🔒 |
+| 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md) | `Database` | Hard | 🔒 |
## Copyright
diff --git a/solution/README.md b/solution/README.md
index ad9470d485719..cfb809c76eb33 100644
--- a/solution/README.md
+++ b/solution/README.md
@@ -3224,6 +3224,7 @@
| 3211 | [生成不含相邻零的二进制字符串](/solution/3200-3299/3211.Generate%20Binary%20Strings%20Without%20Adjacent%20Zeros/README.md) | | 中等 | 第 405 场周赛 |
| 3212 | [统计 X 和 Y 频数相等的子矩阵数量](/solution/3200-3299/3212.Count%20Submatrices%20With%20Equal%20Frequency%20of%20X%20and%20Y/README.md) | | 中等 | 第 405 场周赛 |
| 3213 | [最小代价构造字符串](/solution/3200-3299/3213.Construct%20String%20with%20Minimum%20Cost/README.md) | | 困难 | 第 405 场周赛 |
+| 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md) | `数据库` | 困难 | 🔒 |
## 版权
diff --git a/solution/README_EN.md b/solution/README_EN.md
index 2259c84e04667..260f99c762067 100644
--- a/solution/README_EN.md
+++ b/solution/README_EN.md
@@ -3222,6 +3222,7 @@ Press Control + F(or Command + F on
| 3211 | [Generate Binary Strings Without Adjacent Zeros](/solution/3200-3299/3211.Generate%20Binary%20Strings%20Without%20Adjacent%20Zeros/README_EN.md) | | Medium | Weekly Contest 405 |
| 3212 | [Count Submatrices With Equal Frequency of X and Y](/solution/3200-3299/3212.Count%20Submatrices%20With%20Equal%20Frequency%20of%20X%20and%20Y/README_EN.md) | | Medium | Weekly Contest 405 |
| 3213 | [Construct String with Minimum Cost](/solution/3200-3299/3213.Construct%20String%20with%20Minimum%20Cost/README_EN.md) | | Hard | Weekly Contest 405 |
+| 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md) | `Database` | Hard | 🔒 |
## Copyright