Skip to content

Commit ea13071

Browse files
authored
feat: add weekly contest 382 (#2267)
1 parent 480b0aa commit ea13071

File tree

12 files changed

+617
-0
lines changed

12 files changed

+617
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [3019. 按键变更的次数](https://leetcode.cn/problems/number-of-changing-keys)
2+
3+
[English Version](/solution/3000-3099/3019.Number%20of%20Changing%20Keys/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从<strong> 0</strong> 开始的字符串 <code>s</code> ,该字符串由用户输入。按键变更的定义是:使用与上次使用的按键不同的键。例如 <code>s = "ab"</code> 表示按键变更一次,而 <code>s = "bBBb"</code> 不存在按键变更。</p>
10+
11+
<p>返回用户输入过程中按键变更的次数。</p>
12+
13+
<p><strong>注意:</strong><code>shift</code> 或 <code>caps lock</code> 等修饰键不计入按键变更,也就是说,如果用户先输入字母 <code>'a'</code> 然后输入字母 <code>'A'</code> ,不算作按键变更。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong class="example">示例 1:</strong></p>
18+
19+
<pre>
20+
<strong>输入:</strong>s = "aAbBcC"
21+
<strong>输出:</strong>2
22+
<strong>解释:</strong>
23+
从 s[0] = 'a' 到 s[1] = 'A',不存在按键变更,因为不计入 caps lock 或 shift 。
24+
从 s[1] = 'A' 到 s[2] = 'b',按键变更。
25+
从 s[2] = 'b' 到 s[3] = 'B',不存在按键变更,因为不计入 caps lock 或 shift 。
26+
从 s[3] = 'B' 到 s[4] = 'c',按键变更。
27+
从 s[4] = 'c' 到 s[5] = 'C',不存在按键变更,因为不计入 caps lock 或 shift 。
28+
</pre>
29+
30+
<p><strong class="example">示例 2:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>s = "AaAaAaaA"
34+
<strong>输出:</strong>0
35+
<strong>解释:</strong> 不存在按键变更,因为这个过程中只按下字母 'a' 和 'A' ,不需要进行按键变更。<!-- notionvc: 8849fe75-f31e-41dc-a2e0-b7d33d8427d2 -->
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
44+
<li><code>s</code> 仅由英文大写字母和小写字母组成。</li>
45+
</ul>
46+
47+
## 解法
48+
49+
### 方法一
50+
51+
<!-- tabs:start -->
52+
53+
```python
54+
55+
```
56+
57+
```java
58+
59+
```
60+
61+
```cpp
62+
63+
```
64+
65+
```go
66+
67+
```
68+
69+
<!-- tabs:end -->
70+
71+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# [3019. Number of Changing Keys](https://leetcode.com/problems/number-of-changing-keys)
2+
3+
[中文文档](/solution/3000-3099/3019.Number%20of%20Changing%20Keys/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed </strong>string <code>s</code> typed by a user. Changing a key is defined as using a key different from the last used key. For example, <code>s = &quot;ab&quot;</code> has a change of a key while <code>s = &quot;bBBb&quot;</code> does not have any.</p>
8+
9+
<p>Return <em>the number of times the user had to change the key. </em></p>
10+
11+
<p><strong>Note: </strong>Modifiers like <code>shift</code> or <code>caps lock</code> won&#39;t be counted in changing the key that is if a user typed the letter <code>&#39;a&#39;</code> and then the letter <code>&#39;A&#39;</code> then it will not be considered as a changing of key.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> s = &quot;aAbBcC&quot;
18+
<strong>Output:</strong> 2
19+
<strong>Explanation:</strong>
20+
From s[0] = &#39;a&#39; to s[1] = &#39;A&#39;, there is no change of key as caps lock or shift is not counted.
21+
From s[1] = &#39;A&#39; to s[2] = &#39;b&#39;, there is a change of key.
22+
From s[2] = &#39;b&#39; to s[3] = &#39;B&#39;, there is no change of key as caps lock or shift is not counted.
23+
From s[3] = &#39;B&#39; to s[4] = &#39;c&#39;, there is a change of key.
24+
From s[4] = &#39;c&#39; to s[5] = &#39;C&#39;, there is no change of key as caps lock or shift is not counted.
25+
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> s = &quot;AaAaAaaA&quot;
32+
<strong>Output:</strong> 0
33+
<strong>Explanation:</strong> There is no change of key since only the letters &#39;a&#39; and &#39;A&#39; are<!-- notionvc: 8849fe75-f31e-41dc-a2e0-b7d33d8427d2 --> pressed which does not require change of key.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
41+
<li><code>s</code> consists of only upper case and lower case English letters.</li>
42+
</ul>
43+
44+
## Solutions
45+
46+
### Solution 1
47+
48+
<!-- tabs:start -->
49+
50+
```python
51+
52+
```
53+
54+
```java
55+
56+
```
57+
58+
```cpp
59+
60+
```
61+
62+
```go
63+
64+
```
65+
66+
<!-- tabs:end -->
67+
68+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [3020. 子集中元素的最大数量](https://leetcode.cn/problems/find-the-maximum-number-of-elements-in-subset)
2+
3+
[English Version](/solution/3000-3099/3020.Find%20the%20Maximum%20Number%20of%20Elements%20in%20Subset/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个<strong> 正整数 </strong>数组 <code>nums</code> 。</p>
10+
11+
<p>你需要从数组中选出一个满足下述条件的<span data-keyword="subset">子集</span>:</p>
12+
13+
<ul>
14+
<li>你可以将选中的元素放置在一个下标从 <strong>0</strong> 开始的数组中,并使其遵循以下模式:<code>[x, x<sup>2</sup>, x<sup>4</sup>, ..., x<sup>k/2</sup>, x<sup>k</sup>, x<sup>k/2</sup>, ..., x<sup>4</sup>, x<sup>2</sup>, x]</code>(<strong>注意</strong>,<code>k</code> 可以是任何 <strong>非负</strong> 的 2 的幂)。例如,<code>[2, 4, 16, 4, 2]</code> 和 <code>[3, 9, 3]</code> 都符合这一模式,而 <code>[2, 4, 8, 4, 2]</code> 则不符合。</li>
15+
</ul>
16+
17+
<p>返回满足这些条件的子集中,元素数量的 <strong>最大值 </strong><em>。</em></p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong class="example">示例 1:</strong></p>
22+
23+
<pre>
24+
<strong>输入:</strong>nums = [5,4,1,2,2]
25+
<strong>输出:</strong>3
26+
<strong>解释:</strong>选择子集 {4,2,2} ,将其放在数组 [2,4,2] 中,它遵循该模式,且 2<sup>2</sup> == 4 。因此答案是 3 。
27+
</pre>
28+
29+
<p><strong class="example">示例 2:</strong></p>
30+
31+
<pre>
32+
<strong>输入:</strong>nums = [1,3,2,4]
33+
<strong>输出:</strong>1
34+
<strong>解释:</strong>选择子集 {1},将其放在数组 [1] 中,它遵循该模式。因此答案是 1 。注意我们也可以选择子集 {2} 、{4} 或 {3} ,可能存在多个子集都能得到相同的答案。
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
39+
<p><strong>提示:</strong></p>
40+
41+
<ul>
42+
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
43+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
44+
</ul>
45+
46+
## 解法
47+
48+
### 方法一
49+
50+
<!-- tabs:start -->
51+
52+
```python
53+
54+
```
55+
56+
```java
57+
58+
```
59+
60+
```cpp
61+
62+
```
63+
64+
```go
65+
66+
```
67+
68+
<!-- tabs:end -->
69+
70+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [3020. Find the Maximum Number of Elements in Subset](https://leetcode.com/problems/find-the-maximum-number-of-elements-in-subset)
2+
3+
[中文文档](/solution/3000-3099/3020.Find%20the%20Maximum%20Number%20of%20Elements%20in%20Subset/README.md)
4+
5+
## Description
6+
7+
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>
8+
9+
<p>You need to select a <span data-keyword="subset">subset</span> of <code>nums</code> which satisfies the following condition:</p>
10+
11+
<ul>
12+
<li>You can place the selected elements in a <strong>0-indexed</strong> array such that it follows the pattern: <code>[x, x<sup>2</sup>, x<sup>4</sup>, ..., x<sup>k/2</sup>, x<sup>k</sup>, x<sup>k/2</sup>, ..., x<sup>4</sup>, x<sup>2</sup>, x]</code> (<strong>Note</strong> that <code>k</code> can be be any <strong>non-negative</strong> power of <code>2</code>). For example, <code>[2, 4, 16, 4, 2]</code> and <code>[3, 9, 3]</code> follow the pattern while <code>[2, 4, 8, 4, 2]</code> does not.</li>
13+
</ul>
14+
15+
<p>Return <em>the <strong>maximum</strong> number of elements in a subset that satisfies these conditions.</em></p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> nums = [5,4,1,2,2]
22+
<strong>Output:</strong> 3
23+
<strong>Explanation:</strong> We can select the subset {4,2,2}, which can be placed in the array as [2,4,2] which follows the pattern and 2<sup>2</sup> == 4. Hence the answer is 3.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> nums = [1,3,2,4]
30+
<strong>Output:</strong> 1
31+
<strong>Explanation:</strong> We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {4}, or {3}, there may be multiple subsets which provide the same answer.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
39+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
40+
</ul>
41+
42+
## Solutions
43+
44+
### Solution 1
45+
46+
<!-- tabs:start -->
47+
48+
```python
49+
50+
```
51+
52+
```java
53+
54+
```
55+
56+
```cpp
57+
58+
```
59+
60+
```go
61+
62+
```
63+
64+
<!-- tabs:end -->
65+
66+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [3021. Alice 和 Bob 玩鲜花游戏](https://leetcode.cn/problems/alice-and-bob-playing-flower-game)
2+
3+
[English Version](/solution/3000-3099/3021.Alice%20and%20Bob%20Playing%20Flower%20Game/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Alice 和 Bob 在一个长满鲜花的环形草地玩一个回合制游戏。环形的草地上有一些鲜花,Alice 到&nbsp;Bob 之间顺时针有 <code>x</code>&nbsp;朵鲜花,逆时针有 <code>y</code>&nbsp;朵鲜花。</p>
10+
11+
<p>游戏过程如下:</p>
12+
13+
<ol>
14+
<li>Alice 先行动。</li>
15+
<li>每一次行动中,当前玩家必须选择顺时针或者逆时针,然后在这个方向上摘一朵鲜花。</li>
16+
<li>一次行动结束后,如果所有鲜花都被摘完了,那么 <strong>当前</strong>&nbsp;玩家抓住对手并赢得游戏的胜利。</li>
17+
</ol>
18+
19+
<p>给你两个整数&nbsp;<code>n</code>&nbsp;&nbsp;<code>m</code>&nbsp;,你的任务是求出满足以下条件的所有&nbsp;<code>(x, y)</code>&nbsp;对:</p>
20+
21+
<ul>
22+
<li>按照上述规则,Alice 必须赢得游戏。</li>
23+
<li>Alice 顺时针方向上的鲜花数目&nbsp;<code>x</code>&nbsp;必须在区间&nbsp;<code>[1,n]</code>&nbsp;之间。</li>
24+
<li>Alice 逆时针方向上的鲜花数目 <code>y</code>&nbsp;必须在区间&nbsp;<code>[1,m]</code>&nbsp;之间。</li>
25+
</ul>
26+
27+
<p>请你返回满足题目描述的数对&nbsp;<code>(x, y)</code>&nbsp;的数目。</p>
28+
29+
<p>&nbsp;</p>
30+
31+
<p><strong class="example">示例 1:</strong></p>
32+
33+
<pre>
34+
<b>输入:</b>n = 3, m = 2
35+
<b>输出:</b>3
36+
<b>解释:</b>以下数对满足题目要求:(1,2) ,(3,2) ,(2,1) 。
37+
</pre>
38+
39+
<p><strong class="example">示例 2:</strong></p>
40+
41+
<pre>
42+
<b>输入:</b>n = 1, m = 1
43+
<b>输出:</b>0
44+
<b>解释:</b>没有数对满足题目要求。
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= n, m &lt;= 10<sup>5</sup></code></li>
53+
</ul>
54+
55+
## 解法
56+
57+
### 方法一
58+
59+
<!-- tabs:start -->
60+
61+
```python
62+
63+
```
64+
65+
```java
66+
67+
```
68+
69+
```cpp
70+
71+
```
72+
73+
```go
74+
75+
```
76+
77+
<!-- tabs:end -->
78+
79+
<!-- end -->

0 commit comments

Comments
 (0)