Skip to content

Commit 5f579e6

Browse files
authored
feat: add weekly contest 390 (doocs#2496)
1 parent 9bb6d44 commit 5f579e6

37 files changed

+1682
-15
lines changed

solution/3000-3099/3086.Minimum Moves to Pick K Ones/README.md

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

1111
<p>给你一个下标从 <strong>0</strong> 开始的二进制数组 <code>nums</code>,其长度为 <code>n</code> ;另给你一个 <strong>正整数 </strong><code>k</code> 以及一个 <strong>非负整数 </strong><code>maxChanges</code> 。</p>
1212

13-
<p>灵茶山艾府在玩一个游戏,游戏的目标是让灵茶山艾府使用 <strong>最少 </strong>数量的 <strong>行动 </strong>次数从 <code>nums</code> 中拾起 <code>k</code> 个 1 。游戏开始时,灵茶山艾府可以选择数组 <code>[0, n - 1]</code> 范围内的任何索引<code>index</code> 站立。如果 <code>nums[index] == 1</code> ,灵茶山艾府就会拾起一个 1 ,并且 <code>nums[index]</code> 变成<code>0</code>(这<strong> 不算 </strong>作一次行动)。之后,灵茶山艾府可以执行 <strong>任意数量</strong> 的 <strong>行动</strong>(<strong>包括</strong><strong>零次</strong>),在每次行动中灵茶山艾府必须 <strong>恰好 </strong>执行以下动作之一:</p>
13+
<p>Alice 在玩一个游戏,游戏的目标是让&nbsp;Alice 使用 <strong>最少 </strong>数量的 <strong>行动 </strong>次数从 <code>nums</code> 中拾起 <code>k</code> 个 1 。游戏开始时,Alice 可以选择数组 <code>[0, n - 1]</code> 范围内的任何索引&nbsp;<code>aliceIndex</code> 站立。如果 <code>nums[aliceIndex] == 1</code> ,Alice 会拾起一个 1 ,并且 <code>nums[aliceIndex]</code> 变成<code>0</code>(这<strong> 不算 </strong>作一次行动)。之后,Alice 可以执行 <strong>任意数量</strong> 的 <strong>行动</strong>(<strong>包括</strong><strong>零次</strong>),在每次行动中&nbsp;Alice 必须 <strong>恰好 </strong>执行以下动作之一:</p>
1414

1515
<ul>
16-
<li>选择任意一个下标 <code>j != index</code> 且满足 <code>nums[j] == 0</code> ,然后将 <code>nums[j]</code> 设置为 <code>1</code> 。这个动作最多可以执行 <code>maxChanges</code> 次。</li>
17-
<li>选择任意两个相邻的下标 <code>x</code> 和 <code>y</code>(<code>|x - y| == 1</code>)且满足 <code>nums[x] == 1</code>, <code>nums[y] == 0</code> ,然后交换它们的值(将 <code>nums[y] = 1</code> 和 <code>nums[x] = 0</code>)。如果 <code>y == index</code>,在这次行动后灵茶山艾府拾起一个 1 ,并且 <code>nums[y]</code> 变成 <code>0</code> 。</li>
16+
<li>选择任意一个下标 <code>j != aliceIndex</code> 且满足 <code>nums[j] == 0</code> ,然后将 <code>nums[j]</code> 设置为 <code>1</code> 。这个动作最多可以执行 <code>maxChanges</code> 次。</li>
17+
<li>选择任意两个相邻的下标 <code>x</code> 和 <code>y</code>(<code>|x - y| == 1</code>)且满足 <code>nums[x] == 1</code>, <code>nums[y] == 0</code> ,然后交换它们的值(将 <code>nums[y] = 1</code> 和 <code>nums[x] = 0</code>)。如果 <code>y == aliceIndex</code>,在这次行动后&nbsp;Alice 拾起一个 1 ,并且 <code>nums[y]</code> 变成 <code>0</code> 。</li>
1818
</ul>
1919

20-
<p>返回灵茶山艾府拾起 <strong>恰好 </strong><code>k</code> 个 1 所需的 <strong>最少 </strong>行动次数。</p>
20+
<p>返回&nbsp;Alice 拾起 <strong>恰好 </strong><code>k</code> 个 1 所需的 <strong>最少 </strong>行动次数。</p>
2121

2222
<p>&nbsp;</p>
2323

@@ -28,16 +28,16 @@
2828

2929
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">3</span></p>
3030

31-
<p><strong>解释:</strong>如果游戏开始时灵茶山艾府在 <code>index == 1</code> 的位置上,按照以下步骤执行每个动作,他可以利用 <code>3</code> 次行动拾取 <code>3</code> 个 1 :</p>
31+
<p><strong>解释:</strong>如果游戏开始时&nbsp;Alice 在 <code>aliceIndex == 1</code> 的位置上,按照以下步骤执行每个动作,他可以利用 <code>3</code> 次行动拾取 <code>3</code> 个 1 :</p>
3232

3333
<ul>
34-
<li>游戏开始时灵茶山艾府拾取了一个 1 ,<code>nums[1]</code> 变成了 <code>0</code>。此时 <code>nums</code> 变为 <code>[1,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code> 。</li>
34+
<li>游戏开始时&nbsp;Alice 拾取了一个 1 ,<code>nums[1]</code> 变成了 <code>0</code>。此时 <code>nums</code> 变为 <code>[1,<strong><u>1</u></strong>,1,0,0,1,1,0,0,1]</code> 。</li>
3535
<li>选择 <code>j == 2</code> 并执行第一种类型的动作。<code>nums</code> 变为 <code>[1,<strong><u>0</u></strong>,1,0,0,1,1,0,0,1]</code></li>
36-
<li>选择 <code>x == 2</code> 和 <code>y == 1</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[1,<strong><u>1</u></strong>,0,0,0,1,1,0,0,1]</code> 。由于 <code>y == index</code>,灵茶山艾府拾取了一个 1 ,<code>nums</code> 变为&nbsp; <code>[1,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code> 。</li>
37-
<li>选择 <code>x == 0</code> 和 <code>y == 1</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[0,<strong><u>1</u></strong>,0,0,0,1,1,0,0,1]</code> 。由于 <code>y == index</code>,灵茶山艾府拾取了一个 1 ,<code>nums</code> 变为&nbsp; <code>[0,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code> 。</li>
36+
<li>选择 <code>x == 2</code> 和 <code>y == 1</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[1,<strong><u>1</u></strong>,0,0,0,1,1,0,0,1]</code> 。由于 <code>y == aliceIndex</code>,Alice 拾取了一个 1 ,<code>nums</code> 变为&nbsp; <code>[1,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code> 。</li>
37+
<li>选择 <code>x == 0</code> 和 <code>y == 1</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[0,<strong><u>1</u></strong>,0,0,0,1,1,0,0,1]</code> 。由于 <code>y == aliceIndex</code>,Alice 拾取了一个 1 ,<code>nums</code> 变为&nbsp; <code>[0,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code> 。</li>
3838
</ul>
3939

40-
<p>请注意,灵茶山艾府也可能执行其他的 <code>3</code> 次行动序列达成拾取 <code>3</code> 个 1 。</p>
40+
<p>请注意,Alice 也可能执行其他的 <code>3</code> 次行动序列达成拾取 <code>3</code> 个 1 。</p>
4141
</div>
4242

4343
<p><strong class="example">示例 2:</strong></p>
@@ -47,13 +47,13 @@
4747

4848
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">4</span></p>
4949

50-
<p><strong>解释:</strong>如果游戏开始时灵茶山艾府在 <code>index == 0</code> 的位置上,按照以下步骤执行每个动作,他可以利用 <code>4</code> 次行动拾取 <code>2</code> 个 1 :</p>
50+
<p><strong>解释:</strong>如果游戏开始时&nbsp;Alice 在 <code>aliceIndex == 0</code> 的位置上,按照以下步骤执行每个动作,他可以利用 <code>4</code> 次行动拾取 <code>2</code> 个 1 :</p>
5151

5252
<ul>
5353
<li>选择 <code>j == 1</code> 并执行第一种类型的动作。<code>nums</code> 变为 <code>[<strong><u>0</u></strong>,1,0,0]</code> 。</li>
54-
<li>选择 <code>x == 1</code> 和 <code>y == 0</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[<strong><u>1</u></strong>,0,0,0]</code> 。由于 <code>y == index</code>,灵茶山艾府拾起了一个 1 ,<code>nums</code> 变为 <code>[<strong><u>0</u></strong>,0,0,0]</code> 。</li>
54+
<li>选择 <code>x == 1</code> 和 <code>y == 0</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[<strong><u>1</u></strong>,0,0,0]</code> 。由于 <code>y == aliceIndex</code>,Alice 拾起了一个 1 ,<code>nums</code> 变为 <code>[<strong><u>0</u></strong>,0,0,0]</code> 。</li>
5555
<li>再次选择 <code>j == 1</code> 并执行第一种类型的动作。<code>nums</code> 变为 <code>[<strong><u>0</u></strong>,1,0,0]</code> 。</li>
56-
<li>再次选择 <code>x == 1</code> 和 <code>y == 0</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[<strong><u>1</u></strong>,0,0,0]</code> 。由于<code>y == index</code>,灵茶山艾府拾起了一个 1 ,<code>nums</code> 变为 <code>[<strong><u>0</u></strong>,0,0,0]</code> 。</li>
56+
<li>再次选择 <code>x == 1</code> 和 <code>y == 0</code> ,并执行第二种类型的动作。<code>nums</code> 变为 <code>[<strong><u>1</u></strong>,0,0,0]</code> 。由于<code>y == aliceIndex</code>,Alice 拾起了一个 1 ,<code>nums</code> 变为 <code>[<strong><u>0</u></strong>,0,0,0]</code> 。</li>
5757
</ul>
5858
</div>
5959

solution/3000-3099/3086.Minimum Moves to Pick K Ones/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<p><strong>Explanation:</strong> Alice can pick up <code>3</code> ones in <code>3</code> moves, if Alice performs the following actions in each move when standing at <code>aliceIndex == 1</code>:</p>
2929

3030
<ul>
31-
<li>&nbsp;At the start of the game Alice picks up the one and <code>nums[1]</code> becomes <code>0</code>. <code>nums</code> becomes <code>[1,<strong><u>0</u></strong>,1,0,0,1,1,0,0,1]</code>.</li>
31+
<li>&nbsp;At the start of the game Alice picks up the one and <code>nums[1]</code> becomes <code>0</code>. <code>nums</code> becomes <code>[1,<strong><u>1</u></strong>,1,0,0,1,1,0,0,1]</code>.</li>
3232
<li>Select <code>j == 2</code> and perform an action of the first type. <code>nums</code> becomes <code>[1,<strong><u>0</u></strong>,1,0,0,1,1,0,0,1]</code></li>
3333
<li>Select <code>x == 2</code> and <code>y == 1</code>, and perform an action of the second type. <code>nums</code> becomes <code>[1,<strong><u>1</u></strong>,0,0,0,1,1,0,0,1]</code>. As <code>y == aliceIndex</code>, Alice picks up the one and <code>nums</code> becomes&nbsp;<code>[1,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code>.</li>
3434
<li>Select <code>x == 0</code> and <code>y == 1</code>, and perform an action of the second type. <code>nums</code> becomes <code>[0,<strong><u>1</u></strong>,0,0,0,1,1,0,0,1]</code>. As <code>y == aliceIndex</code>, Alice picks up the one and <code>nums</code> becomes&nbsp;<code>[0,<strong><u>0</u></strong>,0,0,0,1,1,0,0,1]</code>.</li>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# [3089. Find Bursty Behavior](https://leetcode.cn/problems/find-bursty-behavior)
2+
3+
[English Version](/solution/3000-3099/3089.Find%20Bursty%20Behavior/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>Table: <code>Posts</code></p>
12+
13+
<pre>
14+
+-------------+---------+
15+
| Column Name | Type |
16+
+-------------+---------+
17+
| post_id | int |
18+
| user_id | int |
19+
| post_date | date |
20+
+-------------+---------+
21+
post_id is the primary key (column with unique values) for this table.
22+
Each row of this table contains post_id, user_id, and post_date.
23+
</pre>
24+
25+
<p>Write a solution to find users who demonstrate <strong>bursty behavior</strong> in their posting patterns during February <code>2024</code>. <strong>Bursty behavior</strong> is defined as <strong>any</strong> period of <strong>7</strong> <strong>consecutive</strong> days where a user&#39;s posting frequency is <strong>at least twice</strong> to their <strong>average</strong> weekly posting frequency for February <code>2024</code>.</p>
26+
27+
<p><strong>Note:</strong> Only include the dates from February <code>1</code> to February <code>28</code> in your analysis, which means you should count February as having exactly <code>4</code> weeks.</p>
28+
29+
<p>Return <em>the result table orderd by </em><code>user_id</code><em> in </em><strong>ascending</strong><em> order.</em></p>
30+
31+
<p>The result format is in the following example.</p>
32+
33+
<p>&nbsp;</p>
34+
<p><strong class="example">Example:</strong></p>
35+
36+
<div class="example-block">
37+
<p><strong>Input:</strong></p>
38+
39+
<p>Posts table:</p>
40+
41+
<pre class="example-io">
42+
+---------+---------+------------+
43+
| post_id | user_id | post_date |
44+
+---------+---------+------------+
45+
| 1 | 1 | 2024-02-27 |
46+
| 2 | 5 | 2024-02-06 |
47+
| 3 | 3 | 2024-02-25 |
48+
| 4 | 3 | 2024-02-14 |
49+
| 5 | 3 | 2024-02-06 |
50+
| 6 | 2 | 2024-02-25 |
51+
+---------+---------+------------+
52+
</pre>
53+
54+
<p><strong>Output:</strong></p>
55+
56+
<pre class="example-io">
57+
+---------+----------------+------------------+
58+
| user_id | max_7day_posts | avg_weekly_posts |
59+
+---------+----------------+------------------+
60+
| 1 | 1 | 0.2500 |
61+
| 2 | 1 | 0.2500 |
62+
| 5 | 1 | 0.2500 |
63+
+---------+----------------+------------------+
64+
</pre>
65+
66+
<p><strong>Explanation:</strong></p>
67+
68+
<ul>
69+
<li><strong>User 1:</strong> Made only 1 post in February, resulting in an average of 0.25 posts per week and a max of 1 post in any 7-day period.</li>
70+
<li><strong>User 2:</strong> Also made just 1 post, with the same average and max 7-day posting frequency as User 1.</li>
71+
<li><strong>User 5:</strong> Like Users 1 and 2, User 5 made only 1 post throughout February, leading to the same average and max 7-day posting metrics.</li>
72+
<li><strong>User 3:</strong> Although User 3 made more posts than the others (3 posts), they did not reach twice the average weekly posts in their consecutive 7-day window, so they are not listed in the output.</li>
73+
</ul>
74+
75+
<p><b>Note:</b> Output table is ordered by user_id in ascending order.</p>
76+
</div>
77+
78+
## 解法
79+
80+
### 方法一
81+
82+
<!-- tabs:start -->
83+
84+
```sql
85+
86+
```
87+
88+
<!-- tabs:end -->
89+
90+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [3089. Find Bursty Behavior](https://leetcode.com/problems/find-bursty-behavior)
2+
3+
[中文文档](/solution/3000-3099/3089.Find%20Bursty%20Behavior/README.md)
4+
5+
<!-- tags: -->
6+
7+
## Description
8+
9+
<p>Table: <code>Posts</code></p>
10+
11+
<pre>
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| post_id | int |
16+
| user_id | int |
17+
| post_date | date |
18+
+-------------+---------+
19+
post_id is the primary key (column with unique values) for this table.
20+
Each row of this table contains post_id, user_id, and post_date.
21+
</pre>
22+
23+
<p>Write a solution to find users who demonstrate <strong>bursty behavior</strong> in their posting patterns during February <code>2024</code>. <strong>Bursty behavior</strong> is defined as <strong>any</strong> period of <strong>7</strong> <strong>consecutive</strong> days where a user&#39;s posting frequency is <strong>at least twice</strong> to their <strong>average</strong> weekly posting frequency for February <code>2024</code>.</p>
24+
25+
<p><strong>Note:</strong> Only include the dates from February <code>1</code> to February <code>28</code> in your analysis, which means you should count February as having exactly <code>4</code> weeks.</p>
26+
27+
<p>Return <em>the result table orderd by </em><code>user_id</code><em> in </em><strong>ascending</strong><em> order.</em></p>
28+
29+
<p>The result format is in the following example.</p>
30+
31+
<p>&nbsp;</p>
32+
<p><strong class="example">Example:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>Input:</strong></p>
36+
37+
<p>Posts table:</p>
38+
39+
<pre class="example-io">
40+
+---------+---------+------------+
41+
| post_id | user_id | post_date |
42+
+---------+---------+------------+
43+
| 1 | 1 | 2024-02-27 |
44+
| 2 | 5 | 2024-02-06 |
45+
| 3 | 3 | 2024-02-25 |
46+
| 4 | 3 | 2024-02-14 |
47+
| 5 | 3 | 2024-02-06 |
48+
| 6 | 2 | 2024-02-25 |
49+
+---------+---------+------------+
50+
</pre>
51+
52+
<p><strong>Output:</strong></p>
53+
54+
<pre class="example-io">
55+
+---------+----------------+------------------+
56+
| user_id | max_7day_posts | avg_weekly_posts |
57+
+---------+----------------+------------------+
58+
| 1 | 1 | 0.2500 |
59+
| 2 | 1 | 0.2500 |
60+
| 5 | 1 | 0.2500 |
61+
+---------+----------------+------------------+
62+
</pre>
63+
64+
<p><strong>Explanation:</strong></p>
65+
66+
<ul>
67+
<li><strong>User 1:</strong> Made only 1 post in February, resulting in an average of 0.25 posts per week and a max of 1 post in any 7-day period.</li>
68+
<li><strong>User 2:</strong> Also made just 1 post, with the same average and max 7-day posting frequency as User 1.</li>
69+
<li><strong>User 5:</strong> Like Users 1 and 2, User 5 made only 1 post throughout February, leading to the same average and max 7-day posting metrics.</li>
70+
<li><strong>User 3:</strong> Although User 3 made more posts than the others (3 posts), they did not reach twice the average weekly posts in their consecutive 7-day window, so they are not listed in the output.</li>
71+
</ul>
72+
73+
<p><b>Note:</b> Output table is ordered by user_id in ascending order.</p>
74+
</div>
75+
76+
## Solutions
77+
78+
### Solution 1
79+
80+
<!-- tabs:start -->
81+
82+
```sql
83+
84+
```
85+
86+
<!-- tabs:end -->
87+
88+
<!-- end -->

0 commit comments

Comments
 (0)