Skip to content

feat: add sql solution to lc problem: No.3118 #2584

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
Apr 15, 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 @@ -47,7 +47,7 @@

<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either<code>&#39;(&#39;</code> , <code>&#39;)&#39;</code>, or lowercase English letter<code>.</code></li>
<li><code>s[i]</code> is either&nbsp;<code>&#39;(&#39;</code> , <code>&#39;)&#39;</code>, or lowercase English letter.</li>
</ul>

## Solutions
Expand Down
3 changes: 3 additions & 0 deletions solution/2700-2799/2796.Repeat String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<li><code>1 &lt;=&nbsp;str.length &lt;= 1000</code></li>
</ul>

<p>&nbsp;</p>
<strong>Follow up:</strong> Let&#39;s assume, for the sake of simplifying analysis, that concatenating strings is a constant time operation <code>O(1)</code>. With this assumption in mind, can you write an algorithm with a runtime complexity of <code>O(log n)</code>?

## Solutions

### Solution 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

<ul>
<li><code>functions</code>&nbsp;is an array of functions that return promises.</li>
<li><code>ms</code>&nbsp;represents the delay duration in milliseconds. It determines the amount of time to wait before resolving each promise in the new array.</li>
<li><code>ms</code>&nbsp;represents the delay duration in milliseconds. It determines the amount of time to wait before resolving or rejecting each promise in the new array.</li>
</ul>

<p>Each function in the new array should return a promise that resolves after a delay of <code>ms</code>&nbsp;milliseconds, preserving the order of the original <code>functions</code>&nbsp;array. The <code>delayAll</code> function should ensure&nbsp;that each promise from <code>functions</code> is executed with a delay, forming the new array of functions returning delayed promises.</p>
<p>Each function in the new array should return a promise that resolves or rejects after an additional delay of <code>ms</code>&nbsp;milliseconds, preserving the order of the original <code>functions</code>&nbsp;array.</p>

<p>The&nbsp;<code>delayAll</code>&nbsp;function should ensure&nbsp;that each promise from&nbsp;<code>functions</code>&nbsp;is executed with a delay, forming the new array of functions returning delayed promises.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down Expand Up @@ -41,6 +43,18 @@ ms = 70
<strong>Explanation:</strong> The promises from the array would have resolved after 50 ms and 80 ms, but they were delayed by 70 ms, thus 50 ms + 70 ms = 120 ms and 80 ms + 70 ms = 150 ms.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong>
functions = [
&nbsp; () =&gt; new Promise((resolve, reject) =&gt; setTimeout(reject, 20)),
&nbsp; () =&gt; new Promise((resolve, reject) =&gt; setTimeout(reject, 100))
],
ms = 30
<strong>Output: </strong>[50,130]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

<p>You are given a binary array <code>possible</code> of length <code>n</code>.</p>

<p>Danielchandg and Bob are playing a game that consists of <code>n</code> levels. Some of the levels in the game are <strong>impossible</strong> to clear while others can <strong>always</strong> be cleared. In particular, if <code>possible[i] == 0</code>, then the <code>i<sup>th</sup></code> level is <strong>impossible</strong> to clear for <strong>both</strong> the players. A player gains <code>1</code> point on clearing a level and loses <code>1</code> point if the player fails to clear it.</p>
<p>Alice and Bob are playing a game that consists of <code>n</code> levels. Some of the levels in the game are <strong>impossible</strong> to clear while others can <strong>always</strong> be cleared. In particular, if <code>possible[i] == 0</code>, then the <code>i<sup>th</sup></code> level is <strong>impossible</strong> to clear for <strong>both</strong> the players. A player gains <code>1</code> point on clearing a level and loses <code>1</code> point if the player fails to clear it.</p>

<p>At the start of the game, Danielchandg will play some levels in the <strong>given order</strong> starting from the <code>0<sup>th</sup></code> level, after which Bob will play for the rest of the levels.</p>
<p>At the start of the game, Alice will play some levels in the <strong>given order</strong> starting from the <code>0<sup>th</sup></code> level, after which Bob will play for the rest of the levels.</p>

<p>Danielchandg wants to know the <strong>minimum</strong> number of levels he should play to gain more points than Bob, if both players play optimally to <strong>maximize</strong> their points.</p>
<p>Alice wants to know the <strong>minimum</strong> number of levels she should play to gain more points than Bob, if both players play optimally to <strong>maximize</strong> their points.</p>

<p>Return <em>the <strong>minimum</strong> number of levels danielchandg should play to gain more points</em>. <em>If this is <strong>not</strong> possible, return</em> <code>-1</code>.</p>
<p>Return <em>the <strong>minimum</strong> number of levels Alice should play to gain more points</em>. <em>If this is <strong>not</strong> possible, return</em> <code>-1</code>.</p>

<p><strong>Note</strong> that each player must play at least <code>1</code> level.</p>

Expand All @@ -28,15 +28,15 @@

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

<p>Let&#39;s look at all the levels that Danielchandg can play up to:</p>
<p>Let&#39;s look at all the levels that Alice can play up to:</p>

<ul>
<li>If Danielchandg plays only level 0 and Bob plays the rest of the levels, Danielchandg has 1 point, while Bob has -1 + 1 - 1 = -1 point.</li>
<li>If Danielchandg plays till level 1 and Bob plays the rest of the levels, Danielchandg has 1 - 1 = 0 points, while Bob has 1 - 1 = 0 points.</li>
<li>If Danielchandg plays till level 2 and Bob plays the rest of the levels, Danielchandg has 1 - 1 + 1 = 1 point, while Bob has -1 point.</li>
<li>If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has -1 + 1 - 1 = -1 point.</li>
<li>If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 1 - 1 = 0 points, while Bob has 1 - 1 = 0 points.</li>
<li>If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 1 - 1 + 1 = 1 point, while Bob has -1 point.</li>
</ul>

<p>Danielchandg must play a minimum of 1 level to gain more points.</p>
<p>Alice must play a minimum of 1 level to gain more points.</p>
</div>

<p><strong class="example">Example 2:</strong></p>
Expand All @@ -48,16 +48,16 @@

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

<p>Let&#39;s look at all the levels that Danielchandg can play up to:</p>
<p>Let&#39;s look at all the levels that Alice can play up to:</p>

<ul>
<li>If Danielchandg plays only level 0 and Bob plays the rest of the levels, Danielchandg has 1 point, while Bob has 4 points.</li>
<li>If Danielchandg plays till level 1 and Bob plays the rest of the levels, Danielchandg has 2 points, while Bob has 3 points.</li>
<li>If Danielchandg plays till level 2 and Bob plays the rest of the levels, Danielchandg has 3 points, while Bob has 2 points.</li>
<li>If Danielchandg plays till level 3 and Bob plays the rest of the levels, Danielchandg has 4 points, while Bob has 1 point.</li>
<li>If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has 4 points.</li>
<li>If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 2 points, while Bob has 3 points.</li>
<li>If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 3 points, while Bob has 2 points.</li>
<li>If Alice plays till level 3 and Bob plays the rest of the levels, Alice has 4 points, while Bob has 1 point.</li>
</ul>

<p>Danielchandg must play a minimum of 3 levels to gain more points.</p>
<p>Alice must play a minimum of 3 levels to gain more points.</p>
</div>

<p><strong class="example">Example 3:</strong></p>
Expand All @@ -69,7 +69,7 @@

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

<p>The only possible way is for both players to play 1 level each. Danielchandg plays level 0 and loses 1 point. Bob plays level 1 and loses 1 point. As both players have equal points, Danielchandg can&#39;t gain more points than Bob.</p>
<p>The only possible way is for both players to play 1 level each. Alice plays level 0 and loses 1 point. Bob plays level 1 and loses 1 point. As both players have equal points, Alice can&#39;t gain more points than Bob.</p>
</div>

<p>&nbsp;</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<p><strong>Constraints:</strong></p>

<ul>
<li><code>2 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li>
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li>
Expand Down
158 changes: 158 additions & 0 deletions solution/3100-3199/3118.Friday Purchase III/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# [3118. Friday Purchase III](https://leetcode.cn/problems/friday-purchase-iii)

[English Version](/solution/3100-3199/3118.Friday%20Purchase%20III/README_EN.md)

<!-- tags: -->

## 题目描述

<!-- 这里写题目描述 -->

<p>Table: <code>Purchases</code></p>

<pre>
+---------------+------+
| Column Name | Type |
+---------------+------+
| user_id | int |
| purchase_date | date |
| amount_spend | int |
+---------------+------+
(user_id, purchase_date, amount_spend) is the primary key (combination of columns with unique values) for this table.
purchase_date will range from November 1, 2023, to November 30, 2023, inclusive of both dates.
Each row contains user_id, purchase_date, and amount_spend.
</pre>

<p>Table: <code>Users</code></p>

<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| membership | enum |
+-------------+------+
user_id is the primary key for this table.
membership is an ENUM (category) type of (&#39;Standard&#39;, &#39;Premium&#39;, &#39;VIP&#39;).
Each row of this table indicates the user_id, membership type.
</pre>

<p>Write a solution to calculate the <strong>total spending</strong> by <code>Premium</code>&nbsp;and <code>VIP</code> members on <strong>each Friday of every week</strong> in November 2023.&nbsp; If there are <strong>no purchases</strong> on a <strong>particular Friday</strong> by <code>Premium</code> or <code>VIP</code> members, it should be considered as <code>0</code>.</p>

<p>Return <em>the result table</em>&nbsp;<em>ordered by week of the month,&nbsp; and </em><code>membership</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>Purchases table:</p>

<pre class="example-io">
+---------+---------------+--------------+
| user_id | purchase_date | amount_spend |
+---------+---------------+--------------+
| 11 | 2023-11-03 | 1126 |
| 15 | 2023-11-10 | 7473 |
| 17 | 2023-11-17 | 2414 |
| 12 | 2023-11-24 | 9692 |
| 8 | 2023-11-24 | 5117 |
| 1 | 2023-11-24 | 5241 |
| 10 | 2023-11-22 | 8266 |
| 13 | 2023-11-21 | 12000 |
+---------+---------------+--------------+
</pre>

<p>Users table:</p>

<pre class="example-io">
+---------+------------+
| user_id | membership |
+---------+------------+
| 11 | Premium |
| 15 | VIP |
| 17 | Standard |
| 12 | VIP |
| 8 | Premium |
| 1 | VIP |
| 10 | Standard |
| 13 | Premium |
+---------+------------+
</pre>

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

<pre class="example-io">
+---------------+-------------+--------------+
| week_of_month | membership | total_amount |
+---------------+-------------+--------------+
| 1 | Premium | 1126 |
| 1 | VIP | 0 |
| 2 | Premium | 0 |
| 2 | VIP | 7473 |
| 3 | Premium | 0 |
| 3 | VIP | 0 |
| 4 | Premium | 5117 |
| 4 | VIP | 14933 |
+---------------+-------------+--------------+
</pre>

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

<ul>
<li>During the first week of November 2023, a transaction occurred on Friday, 2023-11-03, by a Premium member amounting to $1,126. No transactions were made by VIP members on this day, resulting in a value of 0.</li>
<li>For the second week of November 2023, there was a transaction on Friday, 2023-11-10, and it was made by a VIP member, amounting to $7,473. Since there were no purchases by Premium members that Friday, the output shows 0 for Premium members.</li>
<li>Similarly, during the third week of November 2023, no transactions by Premium or VIP members occurred on Friday, 2023-11-17, which shows 0 for both categories in this week.</li>
<li>In the fourth week of November 2023, transactions occurred on Friday, 2023-11-24, involving one Premium member purchase of $5,117 and VIP member purchases totaling $14,933 ($9,692 from one and $5,241 from another).</li>
</ul>

<p><strong>Note:</strong> The output table is ordered by week_of_month and membership in ascending order.</p>
</div>

## 解法

### 方法一:递归 + 连接

我们首先创建一个递归表 `T`,其中包含 `week_of_month` 列,表示月份的第几周。然后创建一个表 `M`,包含 `membership` 列,表示会员类型,取值为 `'Premium'` 和 `'VIP'`。

接着创建一个表 `P`,包含 `week_of_month`、`membership` 和 `amount_spend` 列,筛选出每个会员在每个月的第几周的周五的消费金额。最后,我们将 `T` 和 `M` 表连接,再左连接 `P` 表,并且按照 `week_of_month` 和 `membership` 列进行分组,计算每周每种会员的总消费金额。

<!-- tabs:start -->

```sql
# Write your MySQL query statement below
WITH RECURSIVE
T AS (
SELECT 1 AS week_of_month
UNION
SELECT week_of_month + 1
FROM T
WHERE week_of_month < 4
),
M AS (
SELECT 'Premium' AS membership
UNION
SELECT 'VIP'
),
P AS (
SELECT CEIL(DAYOFMONTH(purchase_date) / 7) AS week_of_month, membership, amount_spend
FROM
Purchases
JOIN Users USING (user_id)
WHERE DAYOFWEEK(purchase_date) = 6
)
SELECT week_of_month, membership, IFNULL(SUM(amount_spend), 0) AS total_amount
FROM
T
JOIN M
LEFT JOIN P USING (week_of_month, membership)
GROUP BY 1, 2
ORDER BY 1, 2;
```

<!-- tabs:end -->

<!-- end -->
Loading