Skip to content

Commit 9a067fc

Browse files
authored
feat: add sql solution to lc problem: No.3118 (#2584)
No.3118.Friday Purchase III
1 parent 2972388 commit 9a067fc

File tree

12 files changed

+383
-20
lines changed

12 files changed

+383
-20
lines changed

solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

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

5353
## Solutions

solution/2700-2799/2796.Repeat String/README_EN.md

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
<li><code>1 &lt;=&nbsp;str.length &lt;= 1000</code></li>
4444
</ul>
4545

46+
<p>&nbsp;</p>
47+
<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>?
48+
4649
## Solutions
4750

4851
### Solution 1

solution/2800-2899/2821.Delay the Resolution of Each Promise/README_EN.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
<ul>
1212
<li><code>functions</code>&nbsp;is an array of functions that return promises.</li>
13-
<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>
13+
<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>
1414
</ul>
1515

16-
<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>
16+
<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>
17+
18+
<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>
1719

1820
<p>&nbsp;</p>
1921
<p><strong class="example">Example 1:</strong></p>
@@ -41,6 +43,18 @@ ms = 70
4143
<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.
4244
</pre>
4345

46+
<p><strong class="example">Example 3:</strong></p>
47+
48+
<pre>
49+
<strong>Input:</strong>
50+
functions = [
51+
&nbsp; () =&gt; new Promise((resolve, reject) =&gt; setTimeout(reject, 20)),
52+
&nbsp; () =&gt; new Promise((resolve, reject) =&gt; setTimeout(reject, 100))
53+
],
54+
ms = 30
55+
<strong>Output: </strong>[50,130]
56+
</pre>
57+
4458
<p>&nbsp;</p>
4559
<p><strong>Constraints:</strong></p>
4660

solution/3000-3099/3096.Minimum Levels to Gain More Points/README_EN.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

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

11-
<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>
11+
<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>
1212

13-
<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>
13+
<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>
1414

15-
<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>
15+
<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>
1616

17-
<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>
17+
<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>
1818

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

@@ -28,15 +28,15 @@
2828

2929
<p><strong>Explanation:</strong></p>
3030

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

3333
<ul>
34-
<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>
35-
<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>
36-
<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>
34+
<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>
35+
<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>
36+
<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>
3737
</ul>
3838

39-
<p>Danielchandg must play a minimum of 1 level to gain more points.</p>
39+
<p>Alice must play a minimum of 1 level to gain more points.</p>
4040
</div>
4141

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

4949
<p><strong>Explanation:</strong></p>
5050

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

5353
<ul>
54-
<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>
55-
<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>
56-
<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>
57-
<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>
54+
<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>
55+
<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>
56+
<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>
57+
<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>
5858
</ul>
5959

60-
<p>Danielchandg must play a minimum of 3 levels to gain more points.</p>
60+
<p>Alice must play a minimum of 3 levels to gain more points.</p>
6161
</div>
6262

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

7070
<p><strong>Explanation:</strong></p>
7171

72-
<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>
72+
<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>
7373
</div>
7474

7575
<p>&nbsp;</p>

solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<p><strong>Constraints:</strong></p>
7272

7373
<ul>
74-
<li><code>2 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
74+
<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
7575
<li><code>0 &lt;= edges.length &lt;= 10<sup>5</sup></code></li>
7676
<li><code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code></li>
7777
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# [3118. Friday Purchase III](https://leetcode.cn/problems/friday-purchase-iii)
2+
3+
[English Version](/solution/3100-3199/3118.Friday%20Purchase%20III/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>Table: <code>Purchases</code></p>
12+
13+
<pre>
14+
+---------------+------+
15+
| Column Name | Type |
16+
+---------------+------+
17+
| user_id | int |
18+
| purchase_date | date |
19+
| amount_spend | int |
20+
+---------------+------+
21+
(user_id, purchase_date, amount_spend) is the primary key (combination of columns with unique values) for this table.
22+
purchase_date will range from November 1, 2023, to November 30, 2023, inclusive of both dates.
23+
Each row contains user_id, purchase_date, and amount_spend.
24+
</pre>
25+
26+
<p>Table: <code>Users</code></p>
27+
28+
<pre>
29+
+-------------+------+
30+
| Column Name | Type |
31+
+-------------+------+
32+
| user_id | int |
33+
| membership | enum |
34+
+-------------+------+
35+
user_id is the primary key for this table.
36+
membership is an ENUM (category) type of (&#39;Standard&#39;, &#39;Premium&#39;, &#39;VIP&#39;).
37+
Each row of this table indicates the user_id, membership type.
38+
</pre>
39+
40+
<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>
41+
42+
<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>
43+
44+
<p>The result format is in the following example.</p>
45+
46+
<p>&nbsp;</p>
47+
<p><strong class="example">Example:</strong></p>
48+
49+
<div class="example-block">
50+
<p><strong>Input:</strong></p>
51+
52+
<p>Purchases table:</p>
53+
54+
<pre class="example-io">
55+
+---------+---------------+--------------+
56+
| user_id | purchase_date | amount_spend |
57+
+---------+---------------+--------------+
58+
| 11 | 2023-11-03 | 1126 |
59+
| 15 | 2023-11-10 | 7473 |
60+
| 17 | 2023-11-17 | 2414 |
61+
| 12 | 2023-11-24 | 9692 |
62+
| 8 | 2023-11-24 | 5117 |
63+
| 1 | 2023-11-24 | 5241 |
64+
| 10 | 2023-11-22 | 8266 |
65+
| 13 | 2023-11-21 | 12000 |
66+
+---------+---------------+--------------+
67+
</pre>
68+
69+
<p>Users table:</p>
70+
71+
<pre class="example-io">
72+
+---------+------------+
73+
| user_id | membership |
74+
+---------+------------+
75+
| 11 | Premium |
76+
| 15 | VIP |
77+
| 17 | Standard |
78+
| 12 | VIP |
79+
| 8 | Premium |
80+
| 1 | VIP |
81+
| 10 | Standard |
82+
| 13 | Premium |
83+
+---------+------------+
84+
</pre>
85+
86+
<p><strong>Output:</strong></p>
87+
88+
<pre class="example-io">
89+
+---------------+-------------+--------------+
90+
| week_of_month | membership | total_amount |
91+
+---------------+-------------+--------------+
92+
| 1 | Premium | 1126 |
93+
| 1 | VIP | 0 |
94+
| 2 | Premium | 0 |
95+
| 2 | VIP | 7473 |
96+
| 3 | Premium | 0 |
97+
| 3 | VIP | 0 |
98+
| 4 | Premium | 5117 |
99+
| 4 | VIP | 14933 |
100+
+---------------+-------------+--------------+
101+
</pre>
102+
103+
<p><strong>Explanation:</strong></p>
104+
105+
<ul>
106+
<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>
107+
<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>
108+
<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>
109+
<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>
110+
</ul>
111+
112+
<p><strong>Note:</strong> The output table is ordered by week_of_month and membership in ascending order.</p>
113+
</div>
114+
115+
## 解法
116+
117+
### 方法一:递归 + 连接
118+
119+
我们首先创建一个递归表 `T`,其中包含 `week_of_month` 列,表示月份的第几周。然后创建一个表 `M`,包含 `membership` 列,表示会员类型,取值为 `'Premium'``'VIP'`
120+
121+
接着创建一个表 `P`,包含 `week_of_month``membership``amount_spend` 列,筛选出每个会员在每个月的第几周的周五的消费金额。最后,我们将 `T``M` 表连接,再左连接 `P` 表,并且按照 `week_of_month``membership` 列进行分组,计算每周每种会员的总消费金额。
122+
123+
<!-- tabs:start -->
124+
125+
```sql
126+
# Write your MySQL query statement below
127+
WITH RECURSIVE
128+
T AS (
129+
SELECT 1 AS week_of_month
130+
UNION
131+
SELECT week_of_month + 1
132+
FROM T
133+
WHERE week_of_month < 4
134+
),
135+
M AS (
136+
SELECT 'Premium' AS membership
137+
UNION
138+
SELECT 'VIP'
139+
),
140+
P AS (
141+
SELECT CEIL(DAYOFMONTH(purchase_date) / 7) AS week_of_month, membership, amount_spend
142+
FROM
143+
Purchases
144+
JOIN Users USING (user_id)
145+
WHERE DAYOFWEEK(purchase_date) = 6
146+
)
147+
SELECT week_of_month, membership, IFNULL(SUM(amount_spend), 0) AS total_amount
148+
FROM
149+
T
150+
JOIN M
151+
LEFT JOIN P USING (week_of_month, membership)
152+
GROUP BY 1, 2
153+
ORDER BY 1, 2;
154+
```
155+
156+
<!-- tabs:end -->
157+
158+
<!-- end -->

0 commit comments

Comments
 (0)