diff --git a/solution/3000-3099/3019.Number of Changing Keys/README.md b/solution/3000-3099/3019.Number of Changing Keys/README.md new file mode 100644 index 0000000000000..1520c72a35ba8 --- /dev/null +++ b/solution/3000-3099/3019.Number of Changing Keys/README.md @@ -0,0 +1,71 @@ +# [3019. 按键变更的次数](https://leetcode.cn/problems/number-of-changing-keys) + +[English Version](/solution/3000-3099/3019.Number%20of%20Changing%20Keys/README_EN.md) + +## 题目描述 + + + +

给你一个下标从 0 开始的字符串 s ,该字符串由用户输入。按键变更的定义是:使用与上次使用的按键不同的键。例如 s = "ab" 表示按键变更一次,而 s = "bBBb" 不存在按键变更。

+ +

返回用户输入过程中按键变更的次数。

+ +

注意:shiftcaps lock 等修饰键不计入按键变更,也就是说,如果用户先输入字母 'a' 然后输入字母 'A' ,不算作按键变更。

+ +

 

+ +

示例 1:

+ +
+输入:s = "aAbBcC"
+输出:2
+解释: 
+从 s[0] = 'a' 到 s[1] = 'A',不存在按键变更,因为不计入 caps lock 或 shift 。
+从 s[1] = 'A' 到 s[2] = 'b',按键变更。
+从 s[2] = 'b' 到 s[3] = 'B',不存在按键变更,因为不计入 caps lock 或 shift 。
+从 s[3] = 'B' 到 s[4] = 'c',按键变更。
+从 s[4] = 'c' 到 s[5] = 'C',不存在按键变更,因为不计入 caps lock 或 shift 。
+
+ +

示例 2:

+ +
+输入:s = "AaAaAaaA"
+输出:0
+解释: 不存在按键变更,因为这个过程中只按下字母 'a' 和 'A' ,不需要进行按键变更。
+
+ +

 

+ +

提示:

+ + + +## 解法 + +### 方法一 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3019.Number of Changing Keys/README_EN.md b/solution/3000-3099/3019.Number of Changing Keys/README_EN.md new file mode 100644 index 0000000000000..857a7a3b06ff6 --- /dev/null +++ b/solution/3000-3099/3019.Number of Changing Keys/README_EN.md @@ -0,0 +1,68 @@ +# [3019. Number of Changing Keys](https://leetcode.com/problems/number-of-changing-keys) + +[中文文档](/solution/3000-3099/3019.Number%20of%20Changing%20Keys/README.md) + +## Description + +

You are given a 0-indexed string s typed by a user. Changing a key is defined as using a key different from the last used key. For example, s = "ab" has a change of a key while s = "bBBb" does not have any.

+ +

Return the number of times the user had to change the key.

+ +

Note: Modifiers like shift or caps lock won't be counted in changing the key that is if a user typed the letter 'a' and then the letter 'A' then it will not be considered as a changing of key.

+ +

 

+

Example 1:

+ +
+Input: s = "aAbBcC"
+Output: 2
+Explanation: 
+From s[0] = 'a' to s[1] = 'A', there is no change of key as caps lock or shift is not counted.
+From s[1] = 'A' to s[2] = 'b', there is a change of key.
+From s[2] = 'b' to s[3] = 'B', there is no change of key as caps lock or shift is not counted.
+From s[3] = 'B' to s[4] = 'c', there is a change of key.
+From s[4] = 'c' to s[5] = 'C', there is no change of key as caps lock or shift is not counted.
+
+
+ +

Example 2:

+ +
+Input: s = "AaAaAaaA"
+Output: 0
+Explanation: There is no change of key since only the letters 'a' and 'A' are pressed which does not require change of key.
+
+ +

 

+

Constraints:

+ + + +## Solutions + +### Solution 1 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README.md b/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README.md new file mode 100644 index 0000000000000..a5cd8e16bd03a --- /dev/null +++ b/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README.md @@ -0,0 +1,70 @@ +# [3020. 子集中元素的最大数量](https://leetcode.cn/problems/find-the-maximum-number-of-elements-in-subset) + +[English Version](/solution/3000-3099/3020.Find%20the%20Maximum%20Number%20of%20Elements%20in%20Subset/README_EN.md) + +## 题目描述 + + + +

给你一个 正整数 数组 nums

+ +

你需要从数组中选出一个满足下述条件的子集

+ + + +

返回满足这些条件的子集中,元素数量的 最大值

+ +

 

+ +

示例 1:

+ +
+输入:nums = [5,4,1,2,2]
+输出:3
+解释:选择子集 {4,2,2} ,将其放在数组 [2,4,2] 中,它遵循该模式,且 22 == 4 。因此答案是 3 。
+
+ +

示例 2:

+ +
+输入:nums = [1,3,2,4]
+输出:1
+解释:选择子集 {1},将其放在数组 [1] 中,它遵循该模式。因此答案是 1 。注意我们也可以选择子集 {2} 、{4} 或 {3} ,可能存在多个子集都能得到相同的答案。
+
+ +

 

+ +

提示:

+ + + +## 解法 + +### 方法一 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README_EN.md b/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README_EN.md new file mode 100644 index 0000000000000..c7f5a25dce84d --- /dev/null +++ b/solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README_EN.md @@ -0,0 +1,66 @@ +# [3020. Find the Maximum Number of Elements in Subset](https://leetcode.com/problems/find-the-maximum-number-of-elements-in-subset) + +[中文文档](/solution/3000-3099/3020.Find%20the%20Maximum%20Number%20of%20Elements%20in%20Subset/README.md) + +## Description + +

You are given an array of positive integers nums.

+ +

You need to select a subset of nums which satisfies the following condition:

+ + + +

Return the maximum number of elements in a subset that satisfies these conditions.

+ +

 

+

Example 1:

+ +
+Input: nums = [5,4,1,2,2]
+Output: 3
+Explanation: We can select the subset {4,2,2}, which can be placed in the array as [2,4,2] which follows the pattern and 22 == 4. Hence the answer is 3.
+
+ +

Example 2:

+ +
+Input: nums = [1,3,2,4]
+Output: 1
+Explanation: 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. 
+
+ +

 

+

Constraints:

+ + + +## Solutions + +### Solution 1 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md new file mode 100644 index 0000000000000..f6b29d08966ba --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md @@ -0,0 +1,79 @@ +# [3021. Alice 和 Bob 玩鲜花游戏](https://leetcode.cn/problems/alice-and-bob-playing-flower-game) + +[English Version](/solution/3000-3099/3021.Alice%20and%20Bob%20Playing%20Flower%20Game/README_EN.md) + +## 题目描述 + + + +

Alice 和 Bob 在一个长满鲜花的环形草地玩一个回合制游戏。环形的草地上有一些鲜花,Alice 到 Bob 之间顺时针有 x 朵鲜花,逆时针有 y 朵鲜花。

+ +

游戏过程如下:

+ +
    +
  1. Alice 先行动。
  2. +
  3. 每一次行动中,当前玩家必须选择顺时针或者逆时针,然后在这个方向上摘一朵鲜花。
  4. +
  5. 一次行动结束后,如果所有鲜花都被摘完了,那么 当前 玩家抓住对手并赢得游戏的胜利。
  6. +
+ +

给你两个整数 n 和 m ,你的任务是求出满足以下条件的所有 (x, y) 对:

+ + + +

请你返回满足题目描述的数对 (x, y) 的数目。

+ +

 

+ +

示例 1:

+ +
+输入:n = 3, m = 2
+输出:3
+解释:以下数对满足题目要求:(1,2) ,(3,2) ,(2,1) 。
+
+ +

示例 2:

+ +
+输入:n = 1, m = 1
+输出:0
+解释:没有数对满足题目要求。
+
+ +

 

+ +

提示:

+ + + +## 解法 + +### 方法一 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md new file mode 100644 index 0000000000000..cd4969c108bb9 --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md @@ -0,0 +1,75 @@ +# [3021. Alice and Bob Playing Flower Game](https://leetcode.com/problems/alice-and-bob-playing-flower-game) + +[中文文档](/solution/3000-3099/3021.Alice%20and%20Bob%20Playing%20Flower%20Game/README.md) + +## Description + +

Alice and Bob are playing a turn-based game on a circular field surrounded by flowers. The circle represents the field, and there are x flowers in the clockwise direction between Alice and Bob, and y flowers in the anti-clockwise direction between them.

+ +

The game proceeds as follows:

+ +
    +
  1. Alice takes the first turn.
  2. +
  3. In each turn, a player must choose either the clockwise or anti-clockwise direction and pick one flower from that side.
  4. +
  5. At the end of the turn, if there are no flowers left at all, the current player captures their opponent and wins the game.
  6. +
+ +

Given two integers, n and m, the task is to compute the number of possible pairs (x, y) that satisfy the conditions:

+ + + +

Return the number of possible pairs (x, y) that satisfy the conditions mentioned in the statement.

+ +

 

+

Example 1:

+ +
+Input: n = 3, m = 2
+Output: 3
+Explanation: The following pairs satisfy conditions described in the statement: (1,2), (3,2), (2,1).
+
+ +

Example 2:

+ +
+Input: n = 1, m = 1
+Output: 0
+Explanation: No pairs satisfy the conditions described in the statement.
+
+ +

 

+

Constraints:

+ + + +## Solutions + +### Solution 1 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README.md b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README.md new file mode 100644 index 0000000000000..04c05024478da --- /dev/null +++ b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README.md @@ -0,0 +1,85 @@ +# [3022. 给定操作次数内使剩余元素的或值最小](https://leetcode.cn/problems/minimize-or-of-remaining-elements-using-operations) + +[English Version](/solution/3000-3099/3022.Minimize%20OR%20of%20Remaining%20Elements%20Using%20Operations/README_EN.md) + +## 题目描述 + + + +

给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。

+ +

一次操作中,你可以选择 nums 中满足 0 <= i < nums.length - 1 的一个下标 i ,并将 nums[i] 和 nums[i + 1] 替换为数字 nums[i] & nums[i + 1] ,其中 & 表示按位 AND 操作。

+ +

请你返回 至多 k 次操作以内,使 nums 中所有剩余元素按位 OR 结果的 最小值 。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [3,5,3,2,7], k = 2
+输出:3
+解释:执行以下操作:
+1. 将 nums[0] 和 nums[1] 替换为 (nums[0] & nums[1]) ,得到 nums 为 [1,3,2,7] 。
+2. 将 nums[2] 和 nums[3] 替换为 (nums[2] & nums[3]) ,得到 nums 为 [1,3,2] 。
+最终数组的按位或值为 3 。
+3 是 k 次操作以内,可以得到的剩余元素的最小按位或值。
+ +

示例 2:

+ +
+输入:nums = [7,3,15,14,2,8], k = 4
+输出:2
+解释:执行以下操作:
+1. 将 nums[0] 和 nums[1] 替换为 (nums[0] & nums[1]) ,得到 nums 为 [3,15,14,2,8] 。
+2. 将 nums[0] 和 nums[1] 替换为 (nums[0] & nums[1]) ,得到 nums 为 [3,14,2,8] 。
+3. 将 nums[0] 和 nums[1] 替换为 (nums[0] & nums[1]) ,得到 nums 为 [2,2,8] 。
+4. 将 nums[1] 和 nums[2] 替换为 (nums[1] & nums[2]) ,得到 nums 为 [2,0] 。
+最终数组的按位或值为 2 。
+2 是 k 次操作以内,可以得到的剩余元素的最小按位或值。
+
+ +

示例 3:

+ +
+输入:nums = [10,7,10,3,9,14,9,4], k = 1
+输出:15
+解释:不执行任何操作,nums 的按位或值为 15 。
+15 是 k 次操作以内,可以得到的剩余元素的最小按位或值。
+
+ +

 

+ +

提示:

+ + + +## 解法 + +### 方法一 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README_EN.md b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README_EN.md new file mode 100644 index 0000000000000..6803513199684 --- /dev/null +++ b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README_EN.md @@ -0,0 +1,81 @@ +# [3022. Minimize OR of Remaining Elements Using Operations](https://leetcode.com/problems/minimize-or-of-remaining-elements-using-operations) + +[中文文档](/solution/3000-3099/3022.Minimize%20OR%20of%20Remaining%20Elements%20Using%20Operations/README.md) + +## Description + +

You are given a 0-indexed integer array nums and an integer k.

+ +

In one operation, you can pick any index i of nums such that 0 <= i < nums.length - 1 and replace nums[i] and nums[i + 1] with a single occurrence of nums[i] & nums[i + 1], where & represents the bitwise AND operator.

+ +

Return the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.

+ +

 

+

Example 1:

+ +
+Input: nums = [3,5,3,2,7], k = 2
+Output: 3
+Explanation: Let's do the following operations:
+1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [1,3,2,7].
+2. Replace nums[2] and nums[3] with (nums[2] & nums[3]) so that nums becomes equal to [1,3,2].
+The bitwise-or of the final array is 3.
+It can be shown that 3 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
+ +

Example 2:

+ +
+Input: nums = [7,3,15,14,2,8], k = 4
+Output: 2
+Explanation: Let's do the following operations:
+1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,15,14,2,8]. 
+2. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,14,2,8].
+3. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [2,2,8].
+4. Replace nums[1] and nums[2] with (nums[1] & nums[2]) so that nums becomes equal to [2,0].
+The bitwise-or of the final array is 2.
+It can be shown that 2 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
+
+ +

Example 3:

+ +
+Input: nums = [10,7,10,3,9,14,9,4], k = 1
+Output: 15
+Explanation: Without applying any operations, the bitwise-or of nums is 15.
+It can be shown that 15 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
+
+ +

 

+

Constraints:

+ + + +## Solutions + +### Solution 1 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/CONTEST_README.md b/solution/CONTEST_README.md index c0782df8e1c55..83682e8e41040 100644 --- a/solution/CONTEST_README.md +++ b/solution/CONTEST_README.md @@ -22,6 +22,13 @@ ## 往期竞赛 +#### 第 382 场周赛(2024-01-28 10:30, 90 分钟) 参赛人数 3134 + +- [3019. 按键变更的次数](/solution/3000-3099/3019.Number%20of%20Changing%20Keys/README.md) +- [3020. 子集中元素的最大数量](/solution/3000-3099/3020.Find%20the%20Maximum%20Number%20of%20Elements%20in%20Subset/README.md) +- [3021. Alice 和 Bob 玩鲜花游戏](/solution/3000-3099/3021.Alice%20and%20Bob%20Playing%20Flower%20Game/README.md) +- [3022. 给定操作次数内使剩余元素的或值最小](/solution/3000-3099/3022.Minimize%20OR%20of%20Remaining%20Elements%20Using%20Operations/README.md) + #### 第 381 场周赛(2024-01-21 10:30, 90 分钟) 参赛人数 3737 - [3014. 输入单词需要的最少按键次数 I](/solution/3000-3099/3014.Minimum%20Number%20of%20Pushes%20to%20Type%20Word%20I/README.md) diff --git a/solution/CONTEST_README_EN.md b/solution/CONTEST_README_EN.md index a0f15c4c27919..453938a871121 100644 --- a/solution/CONTEST_README_EN.md +++ b/solution/CONTEST_README_EN.md @@ -25,6 +25,13 @@ Get your rating changes right after the completion of LeetCode contests, https:/ ## Past Contests +#### Weekly Contest 382 + +- [3019. Number of Changing Keys](/solution/3000-3099/3019.Number%20of%20Changing%20Keys/README_EN.md) +- [3020. Find the Maximum Number of Elements in Subset](/solution/3000-3099/3020.Find%20the%20Maximum%20Number%20of%20Elements%20in%20Subset/README_EN.md) +- [3021. Alice and Bob Playing Flower Game](/solution/3000-3099/3021.Alice%20and%20Bob%20Playing%20Flower%20Game/README_EN.md) +- [3022. Minimize OR of Remaining Elements Using Operations](/solution/3000-3099/3022.Minimize%20OR%20of%20Remaining%20Elements%20Using%20Operations/README_EN.md) + #### Weekly Contest 381 - [3014. Minimum Number of Pushes to Type Word I](/solution/3000-3099/3014.Minimum%20Number%20of%20Pushes%20to%20Type%20Word%20I/README_EN.md) diff --git a/solution/README.md b/solution/README.md index 218d58e30bab1..ce1a6a6771b4d 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3029,6 +3029,10 @@ | 3016 | [输入单词需要的最少按键次数 II](/solution/3000-3099/3016.Minimum%20Number%20of%20Pushes%20to%20Type%20Word%20II/README.md) | | 中等 | 第 381 场周赛 | | 3017 | [按距离统计房屋对数目 II](/solution/3000-3099/3017.Count%20the%20Number%20of%20Houses%20at%20a%20Certain%20Distance%20II/README.md) | | 困难 | 第 381 场周赛 | | 3018 | [Maximum Number of Removal Queries That Can Be Processed I](/solution/3000-3099/3018.Maximum%20Number%20of%20Removal%20Queries%20That%20Can%20Be%20Processed%20I/README.md) | | 困难 | 🔒 | +| 3019 | [按键变更的次数](/solution/3000-3099/3019.Number%20of%20Changing%20Keys/README.md) | | 简单 | 第 382 场周赛 | +| 3020 | [子集中元素的最大数量](/solution/3000-3099/3020.Find%20the%20Maximum%20Number%20of%20Elements%20in%20Subset/README.md) | | 中等 | 第 382 场周赛 | +| 3021 | [Alice 和 Bob 玩鲜花游戏](/solution/3000-3099/3021.Alice%20and%20Bob%20Playing%20Flower%20Game/README.md) | | 中等 | 第 382 场周赛 | +| 3022 | [给定操作次数内使剩余元素的或值最小](/solution/3000-3099/3022.Minimize%20OR%20of%20Remaining%20Elements%20Using%20Operations/README.md) | | 困难 | 第 382 场周赛 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index bf59ec0029b8d..af2359c92e64f 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3027,6 +3027,10 @@ Press Control + F(or Command + F on | 3016 | [Minimum Number of Pushes to Type Word II](/solution/3000-3099/3016.Minimum%20Number%20of%20Pushes%20to%20Type%20Word%20II/README_EN.md) | | Medium | Weekly Contest 381 | | 3017 | [Count the Number of Houses at a Certain Distance II](/solution/3000-3099/3017.Count%20the%20Number%20of%20Houses%20at%20a%20Certain%20Distance%20II/README_EN.md) | | Hard | Weekly Contest 381 | | 3018 | [Maximum Number of Removal Queries That Can Be Processed I](/solution/3000-3099/3018.Maximum%20Number%20of%20Removal%20Queries%20That%20Can%20Be%20Processed%20I/README_EN.md) | | Hard | 🔒 | +| 3019 | [Number of Changing Keys](/solution/3000-3099/3019.Number%20of%20Changing%20Keys/README_EN.md) | | Easy | Weekly Contest 382 | +| 3020 | [Find the Maximum Number of Elements in Subset](/solution/3000-3099/3020.Find%20the%20Maximum%20Number%20of%20Elements%20in%20Subset/README_EN.md) | | Medium | Weekly Contest 382 | +| 3021 | [Alice and Bob Playing Flower Game](/solution/3000-3099/3021.Alice%20and%20Bob%20Playing%20Flower%20Game/README_EN.md) | | Medium | Weekly Contest 382 | +| 3022 | [Minimize OR of Remaining Elements Using Operations](/solution/3000-3099/3022.Minimize%20OR%20of%20Remaining%20Elements%20Using%20Operations/README_EN.md) | | Hard | Weekly Contest 382 | ## Copyright