Skip to content

feat: add solutions to lc problem: No.3214 #3234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2024
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 @@ -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] {
Expand All @@ -167,7 +167,7 @@ class Solution {
left = mid + 1
}
}

return nums[left]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
165 changes: 165 additions & 0 deletions solution/3200-3299/3214.Year on Year Growth Rate/README.md
Original file line number Diff line number Diff line change
@@ -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:
- 数据库
---

<!-- problem:start -->

# [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)

## 题目描述

<!-- description:start -->

<p>Table: <code>user_transactions</code></p>

<pre>
+------------------+----------+
| 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.
</pre>

<p>Write a solution to calculate the <strong>year-on-year growth rate</strong> for the total spend <strong>for each product</strong>.</p>

<p>The result table should include the following columns:</p>

<ul>
<li><code>year</code>: The year of the transaction.</li>
<li><code>product_id</code>: The ID of the product.</li>
<li><code>curr_year_spend</code>: The total spend for the current year.</li>
<li><code>prev_year_spend</code>: The total spend for the previous year.</li>
<li><code>yoy_rate</code>: The year-on-year growth rate percentage, rounded to <code>2</code> decimal places.</li>
</ul>

<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>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p><code>user_transactions</code> table:</p>

<pre class="example-io">
+----------------+------------+---------+---------------------+
| 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 |
+----------------+------------+---------+---------------------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+------+------------+----------------+----------------+----------+
| 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 |
+------+------------+----------------+----------------+----------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li>For product ID 123424:
<ul>
<li>In 2019:
<ul>
<li>Current year&#39;s spend is 1500.60</li>
<li>No previous year&#39;s spend recorded</li>
<li>YoY growth rate: NULL</li>
</ul>
</li>
<li>In 2020:
<ul>
<li>Current year&#39;s spend is 1000.20</li>
<li>Previous year&#39;s spend is 1500.60</li>
<li>YoY growth rate: ((1000.20 - 1500.60) / 1500.60) * 100 = -33.35%</li>
</ul>
</li>
<li>In 2021:
<ul>
<li>Current year&#39;s spend is 1246.44</li>
<li>Previous year&#39;s spend is 1000.20</li>
<li>YoY growth rate: ((1246.44 - 1000.20) / 1000.20) * 100 = 24.62%</li>
</ul>
</li>
<li>In 2022:
<ul>
<li>Current year&#39;s spend is 2145.32</li>
<li>Previous year&#39;s spend is 1246.44</li>
<li>YoY growth rate: ((2145.32 - 1246.44) / 1246.44) * 100 = 72.12%</li>
</ul>
</li>
</ul>
</li>
</ul>

<p><strong>Note:</strong> Output table is ordered by <code>product_id</code> and <code>year</code> in ascending order.</p>
</div>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:分组统计 + 窗口函数

我们可以先按照年份和产品 ID 进行分组统计每个产品每年的总花费,记录在 `T` 表中。然后使用窗口函数 `LAG` 计算出上一年的总花费,记录在 `S` 表中。最后根据公式计算出年增长率。

<!-- tabs:start -->

#### 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;
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
165 changes: 165 additions & 0 deletions solution/3200-3299/3214.Year on Year Growth Rate/README_EN.md
Original file line number Diff line number Diff line change
@@ -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
---

<!-- problem:start -->

# [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

<!-- description:start -->

<p>Table: <code>user_transactions</code></p>

<pre>
+------------------+----------+
| 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.
</pre>

<p>Write a solution to calculate the <strong>year-on-year growth rate</strong> for the total spend <strong>for each product</strong>.</p>

<p>The result table should include the following columns:</p>

<ul>
<li><code>year</code>: The year of the transaction.</li>
<li><code>product_id</code>: The ID of the product.</li>
<li><code>curr_year_spend</code>: The total spend for the current year.</li>
<li><code>prev_year_spend</code>: The total spend for the previous year.</li>
<li><code>yoy_rate</code>: The year-on-year growth rate percentage, rounded to <code>2</code> decimal places.</li>
</ul>

<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>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p><code>user_transactions</code> table:</p>

<pre class="example-io">
+----------------+------------+---------+---------------------+
| 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 |
+----------------+------------+---------+---------------------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+------+------------+----------------+----------------+----------+
| 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 |
+------+------------+----------------+----------------+----------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li>For product ID 123424:
<ul>
<li>In 2019:
<ul>
<li>Current year&#39;s spend is 1500.60</li>
<li>No previous year&#39;s spend recorded</li>
<li>YoY growth rate: NULL</li>
</ul>
</li>
<li>In 2020:
<ul>
<li>Current year&#39;s spend is 1000.20</li>
<li>Previous year&#39;s spend is 1500.60</li>
<li>YoY growth rate: ((1000.20 - 1500.60) / 1500.60) * 100 = -33.35%</li>
</ul>
</li>
<li>In 2021:
<ul>
<li>Current year&#39;s spend is 1246.44</li>
<li>Previous year&#39;s spend is 1000.20</li>
<li>YoY growth rate: ((1246.44 - 1000.20) / 1000.20) * 100 = 24.62%</li>
</ul>
</li>
<li>In 2022:
<ul>
<li>Current year&#39;s spend is 2145.32</li>
<li>Previous year&#39;s spend is 1246.44</li>
<li>YoY growth rate: ((2145.32 - 1246.44) / 1246.44) * 100 = 72.12%</li>
</ul>
</li>
</ul>
</li>
</ul>

<p><strong>Note:</strong> Output table is ordered by <code>product_id</code> and <code>year</code> in ascending order.</p>
</div>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### 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.

<!-- tabs:start -->

#### 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;
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
22 changes: 22 additions & 0 deletions solution/3200-3299/3214.Year on Year Growth Rate/Solution.sql
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | `数据库` | 困难 | 🔒 |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/DATABASE_README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> 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

Expand Down
Loading
Loading