Skip to content

[pull] main from doocs:main #156

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 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: add solutions to lc problem: No.3214 (doocs#3234)
No.3214.Year on Year Growth Rate
  • Loading branch information
yanglbme authored Jul 9, 2024
commit d3af566e2f3cc2c417391dba04834e5ddb8fc912
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