diff --git a/solution/0300-0399/0383.Ransom Note/README.md b/solution/0300-0399/0383.Ransom Note/README.md index 5c720a3b45638..44cabea0cfd21 100644 --- a/solution/0300-0399/0383.Ransom Note/README.md +++ b/solution/0300-0399/0383.Ransom Note/README.md @@ -136,7 +136,7 @@ func canConstruct(ransomNote string, magazine string) bool { ```ts function canConstruct(ransomNote: string, magazine: string): boolean { - const cnt = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of magazine) { ++cnt[c.charCodeAt(0) - 97]; } diff --git a/solution/0300-0399/0383.Ransom Note/README_EN.md b/solution/0300-0399/0383.Ransom Note/README_EN.md index 2c792b73cd809..e3a3698e4ef77 100644 --- a/solution/0300-0399/0383.Ransom Note/README_EN.md +++ b/solution/0300-0399/0383.Ransom Note/README_EN.md @@ -113,7 +113,7 @@ func canConstruct(ransomNote string, magazine string) bool { ```ts function canConstruct(ransomNote: string, magazine: string): boolean { - const cnt = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of magazine) { ++cnt[c.charCodeAt(0) - 97]; } diff --git a/solution/0300-0399/0383.Ransom Note/Solution.ts b/solution/0300-0399/0383.Ransom Note/Solution.ts index 66a65595b63e0..7fe6ae0705da8 100644 --- a/solution/0300-0399/0383.Ransom Note/Solution.ts +++ b/solution/0300-0399/0383.Ransom Note/Solution.ts @@ -1,5 +1,5 @@ function canConstruct(ransomNote: string, magazine: string): boolean { - const cnt = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of magazine) { ++cnt[c.charCodeAt(0) - 97]; } diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README.md b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README.md new file mode 100644 index 0000000000000..c626bbeccbab7 --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README.md @@ -0,0 +1,160 @@ +# [10031. 大于等于顺序前缀和的最小缺失整数](https://leetcode.cn/problems/smallest-missing-integer-greater-than-sequential-prefix-sum) + +[English Version](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README_EN.md) + +## 题目描述 + + + +

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

+ +

如果一个前缀 nums[0..i] 满足对于 1 <= j <= i 的所有元素都有 nums[j] = nums[j - 1] + 1 ,那么我们称这个前缀是一个 顺序前缀 。特殊情况是,只包含 nums[0] 的前缀也是一个 顺序前缀

+ +

请你返回 nums 中没有出现过的 最小 整数 x ,满足 x 大于等于 最长 顺序前缀的和。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [1,2,3,2,5]
+输出:6
+解释:nums 的最长顺序前缀是 [1,2,3] ,和为 6 ,6 不在数组中,所以 6 是大于等于最长顺序前缀和的最小整数。
+
+ +

示例 2:

+ +
+输入:nums = [3,4,5,1,12,14,13]
+输出:15
+解释:nums 的最长顺序前缀是 [3,4,5] ,和为 12 ,12、13 和 14 都在数组中,但 15 不在,所以 15 是大于等于最长顺序前缀和的最小整数。
+
+ +

 

+ +

提示:

+ + + +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def missingInteger(self, nums: List[int]) -> int: + s, n = nums[0], len(nums) + j = 1 + while j < len(nums) and nums[j] == nums[j - 1] + 1: + s += nums[j] + j += 1 + vis = set(nums) + for x in count(s): + if x not in vis: + return x +``` + +### **Java** + + + +```java +class Solution { + public int missingInteger(int[] nums) { + int s = nums[0], j = 1; + while (j < nums.length && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + boolean[] vis = new boolean[51]; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int missingInteger(vector& nums) { + int s = nums[0], j = 1; + while (j < nums.size() && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + bool vis[51]{}; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= 51 || !vis[x]) { + return x; + } + } + } +}; +``` + +### **Go** + +```go +func missingInteger(nums []int) int { + s, j := nums[0], 1 + for j < len(nums) && nums[j] == nums[j-1]+1 { + s, j = s+nums[j], j+1 + } + vis := [51]bool{} + for _, x := range nums { + vis[x] = true + } + for x := s; ; x++ { + if x >= len(vis) || !vis[x] { + return x + } + } +} +``` + +### **TypeScript** + +```ts +function missingInteger(nums: number[]): number { + let [s, j] = [nums[0], 1]; + const n = nums.length; + while (j < n && nums[j] === nums[j - 1] + 1) { + s += nums[j++]; + } + const vis: boolean[] = Array(51).fill(false); + for (const x of nums) { + vis[x] = true; + } + for (let x = s; ; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README_EN.md b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README_EN.md new file mode 100644 index 0000000000000..9667644b4f00a --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README_EN.md @@ -0,0 +1,150 @@ +# [10031. Smallest Missing Integer Greater Than Sequential Prefix Sum](https://leetcode.com/problems/smallest-missing-integer-greater-than-sequential-prefix-sum) + +[中文文档](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README.md) + +## Description + +

You are given a 0-indexed array of integers nums.

+ +

A prefix nums[0..i] is sequential if, for all 1 <= j <= i, nums[j] = nums[j - 1] + 1. In particular, the prefix consisting only of nums[0] is sequential.

+ +

Return the smallest integer x missing from nums such that x is greater than or equal to the sum of the longest sequential prefix.

+ +

 

+

Example 1:

+ +
+Input: nums = [1,2,3,2,5]
+Output: 6
+Explanation: The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
+
+ +

Example 2:

+ +
+Input: nums = [3,4,5,1,12,14,13]
+Output: 15
+Explanation: The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
+
+ +

 

+

Constraints:

+ + + +## Solutions + + + +### **Python3** + +```python +class Solution: + def missingInteger(self, nums: List[int]) -> int: + s, n = nums[0], len(nums) + j = 1 + while j < len(nums) and nums[j] == nums[j - 1] + 1: + s += nums[j] + j += 1 + vis = set(nums) + for x in count(s): + if x not in vis: + return x +``` + +### **Java** + +```java +class Solution { + public int missingInteger(int[] nums) { + int s = nums[0], j = 1; + while (j < nums.length && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + boolean[] vis = new boolean[51]; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int missingInteger(vector& nums) { + int s = nums[0], j = 1; + while (j < nums.size() && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + bool vis[51]{}; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= 51 || !vis[x]) { + return x; + } + } + } +}; +``` + +### **Go** + +```go +func missingInteger(nums []int) int { + s, j := nums[0], 1 + for j < len(nums) && nums[j] == nums[j-1]+1 { + s, j = s+nums[j], j+1 + } + vis := [51]bool{} + for _, x := range nums { + vis[x] = true + } + for x := s; ; x++ { + if x >= len(vis) || !vis[x] { + return x + } + } +} +``` + +### **TypeScript** + +```ts +function missingInteger(nums: number[]): number { + let [s, j] = [nums[0], 1]; + const n = nums.length; + while (j < n && nums[j] === nums[j - 1] + 1) { + s += nums[j++]; + } + const vis: boolean[] = Array(51).fill(false); + for (const x of nums) { + vis[x] = true; + } + for (let x = s; ; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.cpp b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.cpp new file mode 100644 index 0000000000000..6081ba54c6b44 --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int missingInteger(vector& nums) { + int s = nums[0], j = 1; + while (j < nums.size() && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + bool vis[51]{}; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= 51 || !vis[x]) { + return x; + } + } + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.go b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.go new file mode 100644 index 0000000000000..26f4a6d360c78 --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.go @@ -0,0 +1,15 @@ +func missingInteger(nums []int) int { + s, j := nums[0], 1 + for j < len(nums) && nums[j] == nums[j-1]+1 { + s, j = s+nums[j], j+1 + } + vis := [51]bool{} + for _, x := range nums { + vis[x] = true + } + for x := s; ; x++ { + if x >= len(vis) || !vis[x] { + return x + } + } +} \ No newline at end of file diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.java b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.java new file mode 100644 index 0000000000000..14710bee62193 --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.java @@ -0,0 +1,17 @@ +class Solution { + public int missingInteger(int[] nums) { + int s = nums[0], j = 1; + while (j < nums.length && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + boolean[] vis = new boolean[51]; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } + } +} \ No newline at end of file diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.py b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.py new file mode 100644 index 0000000000000..70bc58d87facf --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def missingInteger(self, nums: List[int]) -> int: + s, n = nums[0], len(nums) + j = 1 + while j < len(nums) and nums[j] == nums[j - 1] + 1: + s += nums[j] + j += 1 + vis = set(nums) + for x in count(s): + if x not in vis: + return x diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.ts b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.ts new file mode 100644 index 0000000000000..97932f3652710 --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.ts @@ -0,0 +1,16 @@ +function missingInteger(nums: number[]): number { + let [s, j] = [nums[0], 1]; + const n = nums.length; + while (j < n && nums[j] === nums[j - 1] + 1) { + s += nums[j++]; + } + const vis: boolean[] = Array(51).fill(false); + for (const x of nums) { + vis[x] = true; + } + for (let x = s; ; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } +} diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README.md b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README.md new file mode 100644 index 0000000000000..9630bdd603fe3 --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README.md @@ -0,0 +1,151 @@ +# [10032. 使数组异或和等于 K 的最少操作次数](https://leetcode.cn/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k) + +[English Version](/solution/10000-10099/10032.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README_EN.md) + +## 题目描述 + + + +

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

+ +

你可以对数组执行以下操作 任意次 :

+ +
    +
  • 选择数组里的 任意 一个元素,并将它的 二进制 表示 翻转 一个数位,翻转数位表示将 0 变成 1 或者将 1 变成 0 。
  • +
+ +

你的目标是让数组里 所有 元素的按位异或和得到 k ,请你返回达成这一目标的 最少 操作次数。

+ +

注意,你也可以将一个数的前导 0 翻转。比方说,数字 (101)2 翻转第四个数位,得到 (1101)2 。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [2,1,3,4], k = 1
+输出:2
+解释:我们可以执行以下操作:
+- 选择下标为 2 的元素,也就是 3 == (011)2 ,我们翻转第一个数位得到 (010)2 == 2 。数组变为 [2,1,2,4] 。
+- 选择下标为 0 的元素,也就是 2 == (010)2 ,我们翻转第三个数位得到 (110)2 == 6 。数组变为 [6,1,2,4] 。
+最终数组的所有元素异或和为 (6 XOR 1 XOR 2 XOR 4) == 1 == k 。
+无法用少于 2 次操作得到异或和等于 k 。
+
+ +

示例 2:

+ +
+输入:nums = [2,0,2,0], k = 0
+输出:0
+解释:数组所有元素的异或和为 (2 XOR 0 XOR 2 XOR 0) == 0 == k 。所以不需要进行任何操作。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 106
  • +
  • 0 <= k <= 106
  • +
+ +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def minOperations(self, nums: List[int], k: int) -> int: + ans = 0 + for i in range(20): + v = 0 + for x in nums: + v ^= x >> i & 1 + ans += (k >> i & 1) != v + return ans +``` + +### **Java** + + + +```java +class Solution { + public int minOperations(int[] nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int minOperations(vector& nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +}; +``` + +### **Go** + +```go +func minOperations(nums []int, k int) (ans int) { + for i := 0; i < 20; i++ { + v := 0 + for _, x := range nums { + v ^= x >> i & 1 + } + ans += k>>i&1 ^ v + } + return +} +``` + +### **TypeScript** + +```ts +function minOperations(nums: number[], k: number): number { + let ans = 0; + for (let i = 0; i < 20; ++i) { + let v = 0; + for (const x of nums) { + v ^= (x >> i) & 1; + } + ans += ((k >> i) & 1) ^ v; + } + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md new file mode 100644 index 0000000000000..1d44e8a3661ef --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md @@ -0,0 +1,141 @@ +# [10032. Minimum Number of Operations to Make Array XOR Equal to K](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k) + +[中文文档](/solution/10000-10099/10032.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README.md) + +## Description + +

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

+ +

You can apply the following operation on the array any number of times:

+ +
    +
  • Choose any element of the array and flip a bit in its binary representation. Flipping a bit means changing a 0 to 1 or vice versa.
  • +
+ +

Return the minimum number of operations required to make the bitwise XOR of all elements of the final array equal to k.

+ +

Note that you can flip leading zero bits in the binary representation of elements. For example, for the number (101)2 you can flip the fourth bit and obtain (1101)2.

+ +

 

+

Example 1:

+ +
+Input: nums = [2,1,3,4], k = 1
+Output: 2
+Explanation: We can do the following operations:
+- Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4].
+- Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4].
+The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.
+It can be shown that we cannot make the XOR equal to k in less than 2 operations.
+
+ +

Example 2:

+ +
+Input: nums = [2,0,2,0], k = 0
+Output: 0
+Explanation: The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • 0 <= nums[i] <= 106
  • +
  • 0 <= k <= 106
  • +
+ +## Solutions + + + +### **Python3** + +```python +class Solution: + def minOperations(self, nums: List[int], k: int) -> int: + ans = 0 + for i in range(20): + v = 0 + for x in nums: + v ^= x >> i & 1 + ans += (k >> i & 1) != v + return ans +``` + +### **Java** + +```java +class Solution { + public int minOperations(int[] nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int minOperations(vector& nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +}; +``` + +### **Go** + +```go +func minOperations(nums []int, k int) (ans int) { + for i := 0; i < 20; i++ { + v := 0 + for _, x := range nums { + v ^= x >> i & 1 + } + ans += k>>i&1 ^ v + } + return +} +``` + +### **TypeScript** + +```ts +function minOperations(nums: number[], k: number): number { + let ans = 0; + for (let i = 0; i < 20; ++i) { + let v = 0; + for (const x of nums) { + v ^= (x >> i) & 1; + } + ans += ((k >> i) & 1) ^ v; + } + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.cpp b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.cpp new file mode 100644 index 0000000000000..7717cfa06fc7d --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int minOperations(vector& nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.go b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.go new file mode 100644 index 0000000000000..a8c6b5278ac66 --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.go @@ -0,0 +1,10 @@ +func minOperations(nums []int, k int) (ans int) { + for i := 0; i < 20; i++ { + v := 0 + for _, x := range nums { + v ^= x >> i & 1 + } + ans += k>>i&1 ^ v + } + return +} \ No newline at end of file diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.java b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.java new file mode 100644 index 0000000000000..5c13d4f76094b --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public int minOperations(int[] nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.py b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.py new file mode 100644 index 0000000000000..92fdc83805505 --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def minOperations(self, nums: List[int], k: int) -> int: + ans = 0 + for i in range(20): + v = 0 + for x in nums: + v ^= x >> i & 1 + ans += (k >> i & 1) != v + return ans diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.ts b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.ts new file mode 100644 index 0000000000000..142deff264ddb --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.ts @@ -0,0 +1,11 @@ +function minOperations(nums: number[], k: number): number { + let ans = 0; + for (let i = 0; i < 20; ++i) { + let v = 0; + for (const x of nums) { + v ^= (x >> i) & 1; + } + ans += ((k >> i) & 1) ^ v; + } + return ans; +} diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README.md b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README.md new file mode 100644 index 0000000000000..ed84d0115a5e6 --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README.md @@ -0,0 +1,210 @@ +# [10033. 使 X 和 Y 相等的最少操作次数](https://leetcode.cn/problems/minimum-number-of-operations-to-make-x-and-y-equal) + +[English Version](/solution/10000-10099/10033.Minimum%20Number%20of%20Operations%20to%20Make%20X%20and%20Y%20Equal/README_EN.md) + +## 题目描述 + + + +

给你两个正整数 x 和 y 。

+ +

一次操作中,你可以执行以下四种操作之一:

+ +
    +
  1. 如果 x 是 11 的倍数,将 x 除以 11 。
  2. +
  3. 如果 x 是 5 的倍数,将 x 除以 5 。
  4. +
  5. 将 x 减 1 。
  6. +
  7. 将 x 加 1 。
  8. +
+ +

请你返回让 x 和 y 相等的 最少 操作次数。

+ +

 

+ +

示例 1:

+ +
+输入:x = 26, y = 1
+输出:3
+解释我们可以通过以下操作将 26 变为 1 :
+1. 将 x 减 1
+2. 将 x 除以 5
+3. 将 x 除以 5
+将 26 变为 1 最少需要 3 次操作。
+
+ +

示例 2:

+ +
+输入:x = 54, y = 2
+输出:4
+解释:我们可以通过以下操作将 54 变为 2 :
+1. 将 x 加 1
+2. 将 x 除以 11
+3. 将 x 除以 5
+4. 将 x 加 1
+将 54 变为 2 最少需要 4 次操作。
+
+ +

示例 3:

+ +
+输入:x = 25, y = 30
+输出:5
+解释:我们可以通过以下操作将 25 变为 30 :
+1. 将 x 加 1
+2. 将 x 加 1
+3. 将 x 加 1
+4. 将 x 加 1
+5. 将 x 加 1
+将 25 变为 30 最少需要 5 次操作。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= x, y <= 104
  • +
+ +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def minimumOperationsToMakeEqual(self, x: int, y: int) -> int: + @cache + def dfs(x: int) -> int: + if y >= x: + return y - x + ans = x - y + ans = min(ans, x % 5 + 1 + dfs(x // 5)) + ans = min(ans, 5 - x % 5 + 1 + dfs(x // 5 + 1)) + ans = min(ans, x % 11 + 1 + dfs(x // 11)) + ans = min(ans, 11 - x % 11 + 1 + dfs(x // 11 + 1)) + return ans + + return dfs(x) +``` + +### **Java** + + + +```java +class Solution { + private Map f = new HashMap<>(); + private int y; + + public int minimumOperationsToMakeEqual(int x, int y) { + this.y = y; + return dfs(x); + } + + private int dfs(int x) { + if (y >= x) { + return y - x; + } + if (f.containsKey(x)) { + return f.get(x); + } + int ans = x - y; + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + ans = Math.min(ans, Math.min(a, Math.min(b, Math.min(c, d)))); + f.put(x, ans); + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int minimumOperationsToMakeEqual(int x, int y) { + unordered_map f; + function dfs = [&](int x) { + if (y >= x) { + return y - x; + } + if (f.count(x)) { + return f[x]; + } + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + return f[x] = min({x - y, a, b, c, d}); + }; + return dfs(x); + } +}; +``` + +### **Go** + +```go +func minimumOperationsToMakeEqual(x int, y int) int { + f := map[int]int{} + var dfs func(int) int + dfs = func(x int) int { + if y >= x { + return y - x + } + if v, ok := f[x]; ok { + return v + } + a := x%5 + 1 + dfs(x/5) + b := 5 - x%5 + 1 + dfs(x/5+1) + c := x%11 + 1 + dfs(x/11) + d := 11 - x%11 + 1 + dfs(x/11+1) + f[x] = min(x-y, a, b, c, d) + return f[x] + } + return dfs(x) +} +``` + +### **TypeScript** + +```ts +function minimumOperationsToMakeEqual(x: number, y: number): number { + const f: Map = new Map(); + const dfs = (x: number): number => { + if (y >= x) { + return y - x; + } + if (f.has(x)) { + return f.get(x)!; + } + const a = (x % 5) + 1 + dfs((x / 5) | 0); + const b = 5 - (x % 5) + 1 + dfs(((x / 5) | 0) + 1); + const c = (x % 11) + 1 + dfs((x / 11) | 0); + const d = 11 - (x % 11) + 1 + dfs(((x / 11) | 0) + 1); + const ans = Math.min(x - y, a, b, c, d); + f.set(x, ans); + return ans; + }; + return dfs(x); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README_EN.md b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README_EN.md new file mode 100644 index 0000000000000..e88607cad372f --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README_EN.md @@ -0,0 +1,200 @@ +# [10033. Minimum Number of Operations to Make X and Y Equal](https://leetcode.com/problems/minimum-number-of-operations-to-make-x-and-y-equal) + +[中文文档](/solution/10000-10099/10033.Minimum%20Number%20of%20Operations%20to%20Make%20X%20and%20Y%20Equal/README.md) + +## Description + +

You are given two positive integers x and y.

+ +

In one operation, you can do one of the four following operations:

+ +
    +
  1. Divide x by 11 if x is a multiple of 11.
  2. +
  3. Divide x by 5 if x is a multiple of 5.
  4. +
  5. Decrement x by 1.
  6. +
  7. Increment x by 1.
  8. +
+ +

Return the minimum number of operations required to make x and y equal.

+ +

 

+

Example 1:

+ +
+Input: x = 26, y = 1
+Output: 3
+Explanation: We can make 26 equal to 1 by applying the following operations: 
+1. Decrement x by 1
+2. Divide x by 5
+3. Divide x by 5
+It can be shown that 3 is the minimum number of operations required to make 26 equal to 1.
+
+ +

Example 2:

+ +
+Input: x = 54, y = 2
+Output: 4
+Explanation: We can make 54 equal to 2 by applying the following operations: 
+1. Increment x by 1
+2. Divide x by 11 
+3. Divide x by 5
+4. Increment x by 1
+It can be shown that 4 is the minimum number of operations required to make 54 equal to 2.
+
+ +

Example 3:

+ +
+Input: x = 25, y = 30
+Output: 5
+Explanation: We can make 25 equal to 30 by applying the following operations: 
+1. Increment x by 1
+2. Increment x by 1
+3. Increment x by 1
+4. Increment x by 1
+5. Increment x by 1
+It can be shown that 5 is the minimum number of operations required to make 25 equal to 30.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= x, y <= 104
  • +
+ +## Solutions + + + +### **Python3** + +```python +class Solution: + def minimumOperationsToMakeEqual(self, x: int, y: int) -> int: + @cache + def dfs(x: int) -> int: + if y >= x: + return y - x + ans = x - y + ans = min(ans, x % 5 + 1 + dfs(x // 5)) + ans = min(ans, 5 - x % 5 + 1 + dfs(x // 5 + 1)) + ans = min(ans, x % 11 + 1 + dfs(x // 11)) + ans = min(ans, 11 - x % 11 + 1 + dfs(x // 11 + 1)) + return ans + + return dfs(x) +``` + +### **Java** + +```java +class Solution { + private Map f = new HashMap<>(); + private int y; + + public int minimumOperationsToMakeEqual(int x, int y) { + this.y = y; + return dfs(x); + } + + private int dfs(int x) { + if (y >= x) { + return y - x; + } + if (f.containsKey(x)) { + return f.get(x); + } + int ans = x - y; + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + ans = Math.min(ans, Math.min(a, Math.min(b, Math.min(c, d)))); + f.put(x, ans); + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int minimumOperationsToMakeEqual(int x, int y) { + unordered_map f; + function dfs = [&](int x) { + if (y >= x) { + return y - x; + } + if (f.count(x)) { + return f[x]; + } + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + return f[x] = min({x - y, a, b, c, d}); + }; + return dfs(x); + } +}; +``` + +### **Go** + +```go +func minimumOperationsToMakeEqual(x int, y int) int { + f := map[int]int{} + var dfs func(int) int + dfs = func(x int) int { + if y >= x { + return y - x + } + if v, ok := f[x]; ok { + return v + } + a := x%5 + 1 + dfs(x/5) + b := 5 - x%5 + 1 + dfs(x/5+1) + c := x%11 + 1 + dfs(x/11) + d := 11 - x%11 + 1 + dfs(x/11+1) + f[x] = min(x-y, a, b, c, d) + return f[x] + } + return dfs(x) +} +``` + +### **TypeScript** + +```ts +function minimumOperationsToMakeEqual(x: number, y: number): number { + const f: Map = new Map(); + const dfs = (x: number): number => { + if (y >= x) { + return y - x; + } + if (f.has(x)) { + return f.get(x)!; + } + const a = (x % 5) + 1 + dfs((x / 5) | 0); + const b = 5 - (x % 5) + 1 + dfs(((x / 5) | 0) + 1); + const c = (x % 11) + 1 + dfs((x / 11) | 0); + const d = 11 - (x % 11) + 1 + dfs(((x / 11) | 0) + 1); + const ans = Math.min(x - y, a, b, c, d); + f.set(x, ans); + return ans; + }; + return dfs(x); +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.cpp b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.cpp new file mode 100644 index 0000000000000..5f739d9aa3f3f --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int minimumOperationsToMakeEqual(int x, int y) { + unordered_map f; + function dfs = [&](int x) { + if (y >= x) { + return y - x; + } + if (f.count(x)) { + return f[x]; + } + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + return f[x] = min({x - y, a, b, c, d}); + }; + return dfs(x); + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.go b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.go new file mode 100644 index 0000000000000..f06dea6c22c6f --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.go @@ -0,0 +1,19 @@ +func minimumOperationsToMakeEqual(x int, y int) int { + f := map[int]int{} + var dfs func(int) int + dfs = func(x int) int { + if y >= x { + return y - x + } + if v, ok := f[x]; ok { + return v + } + a := x%5 + 1 + dfs(x/5) + b := 5 - x%5 + 1 + dfs(x/5+1) + c := x%11 + 1 + dfs(x/11) + d := 11 - x%11 + 1 + dfs(x/11+1) + f[x] = min(x-y, a, b, c, d) + return f[x] + } + return dfs(x) +} \ No newline at end of file diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.java b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.java new file mode 100644 index 0000000000000..8faf975eabce1 --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.java @@ -0,0 +1,26 @@ +class Solution { + private Map f = new HashMap<>(); + private int y; + + public int minimumOperationsToMakeEqual(int x, int y) { + this.y = y; + return dfs(x); + } + + private int dfs(int x) { + if (y >= x) { + return y - x; + } + if (f.containsKey(x)) { + return f.get(x); + } + int ans = x - y; + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + ans = Math.min(ans, Math.min(a, Math.min(b, Math.min(c, d)))); + f.put(x, ans); + return ans; + } +} \ No newline at end of file diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.py b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.py new file mode 100644 index 0000000000000..bcfbce832d4ef --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def minimumOperationsToMakeEqual(self, x: int, y: int) -> int: + @cache + def dfs(x: int) -> int: + if y >= x: + return y - x + ans = x - y + ans = min(ans, x % 5 + 1 + dfs(x // 5)) + ans = min(ans, 5 - x % 5 + 1 + dfs(x // 5 + 1)) + ans = min(ans, x % 11 + 1 + dfs(x // 11)) + ans = min(ans, 11 - x % 11 + 1 + dfs(x // 11 + 1)) + return ans + + return dfs(x) diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.ts b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.ts new file mode 100644 index 0000000000000..27a3b8492f4cd --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.ts @@ -0,0 +1,19 @@ +function minimumOperationsToMakeEqual(x: number, y: number): number { + const f: Map = new Map(); + const dfs = (x: number): number => { + if (y >= x) { + return y - x; + } + if (f.has(x)) { + return f.get(x)!; + } + const a = (x % 5) + 1 + dfs((x / 5) | 0); + const b = 5 - (x % 5) + 1 + dfs(((x / 5) | 0) + 1); + const c = (x % 11) + 1 + dfs((x / 11) | 0); + const d = 11 - (x % 11) + 1 + dfs(((x / 11) | 0) + 1); + const ans = Math.min(x - y, a, b, c, d); + f.set(x, ans); + return ans; + }; + return dfs(x); +} diff --git a/solution/10000-10099/10034.Count the Number of Powerful Integers/README.md b/solution/10000-10099/10034.Count the Number of Powerful Integers/README.md new file mode 100644 index 0000000000000..9961b58c40a81 --- /dev/null +++ b/solution/10000-10099/10034.Count the Number of Powerful Integers/README.md @@ -0,0 +1,279 @@ +# [10034. 统计强大整数的数目](https://leetcode.cn/problems/count-the-number-of-powerful-integers) + +[English Version](/solution/10000-10099/10034.Count%20the%20Number%20of%20Powerful%20Integers/README_EN.md) + +## 题目描述 + + + +

给你三个整数 start ,finish 和 limit 。同时给你一个下标从 0 开始的字符串 s ,表示一个  整数。

+ +

如果一个  整数 x 末尾部分是 s (换句话说,s 是 x 的 后缀),且 x 中的每个数位至多是 limit ,那么我们称 x 是 强大的 。

+ +

请你返回区间 [start..finish] 内强大整数的 总数目 。

+ +

如果一个字符串 x 是 y 中某个下标开始(包括 0 ),到下标为 y.length - 1 结束的子字符串,那么我们称 x 是 y 的一个后缀。比方说,25 是 5125 的一个后缀,但不是 512 的后缀。

+ +

 

+ +

示例 1:

+ +
+输入:start = 1, finish = 6000, limit = 4, s = "124"
+输出:5
+解释:区间 [1..6000] 内的强大数字为 124 ,1124 ,2124 ,3124 和 4124 。这些整数的各个数位都 <= 4 且 "124" 是它们的后缀。注意 5124 不是强大整数,因为第一个数位 5 大于 4 。
+这个区间内总共只有这 5 个强大整数。
+
+ +

示例 2:

+ +
+输入:start = 15, finish = 215, limit = 6, s = "10"
+输出:2
+解释:区间 [15..215] 内的强大整数为 110 和 210 。这些整数的各个数位都 <= 6 且 "10" 是它们的后缀。
+这个区间总共只有这 2 个强大整数。
+
+ +

示例 3:

+ +
+输入:start = 1000, finish = 2000, limit = 4, s = "3000"
+输出:0
+解释:区间 [1000..2000] 内的整数都小于 3000 ,所以 "3000" 不可能是这个区间内任何整数的后缀。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= start <= finish <= 1015
  • +
  • 1 <= limit <= 9
  • +
  • 1 <= s.length <= floor(log10(finish)) + 1
  • +
  • s 数位中每个数字都小于等于 limit 。
  • +
  • s 不包含任何前导 0 。
  • +
+ +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int: + @cache + def dfs(pos: int, lim: int): + if len(t) < n: + return 0 + if len(t) - pos == n: + return int(s <= t[pos:]) if lim else 1 + up = min(int(t[pos]) if lim else 9, limit) + ans = 0 + for i in range(up + 1): + ans += dfs(pos + 1, lim and i == int(t[pos])) + return ans + + n = len(s) + t = str(start - 1) + a = dfs(0, True) + dfs.cache_clear() + t = str(finish) + b = dfs(0, True) + return b - a +``` + +### **Java** + + + +```java +class Solution { + private String s; + private String t; + private Long[] f; + private int limit; + + public long numberOfPowerfulInt(long start, long finish, int limit, String s) { + this.s = s; + this.limit = limit; + t = String.valueOf(start - 1); + f = new Long[20]; + long a = dfs(0, true); + t = String.valueOf(finish); + f = new Long[20]; + long b = dfs(0, true); + return b - a; + } + + private long dfs(int pos, boolean lim) { + if (t.length() < s.length()) { + return 0; + } + if (!lim && f[pos] != null) { + return f[pos]; + } + if (t.length() - pos == s.length()) { + return lim ? (s.compareTo(t.substring(pos)) <= 0 ? 1 : 0) : 1; + } + int up = lim ? t.charAt(pos) - '0' : 9; + up = Math.min(up, limit); + long ans = 0; + for (int i = 0; i <= up; ++i) { + ans += dfs(pos + 1, lim && i == (t.charAt(pos) - '0')); + } + if (!lim) { + f[pos] = ans; + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long numberOfPowerfulInt(long long start, long long finish, int limit, string s) { + string t = to_string(start - 1); + long long f[20]; + memset(f, -1, sizeof(f)); + + function dfs = [&](int pos, bool lim) -> long long { + if (t.size() < s.size()) { + return 0; + } + if (!lim && f[pos] != -1) { + return f[pos]; + } + if (t.size() - pos == s.size()) { + return lim ? s <= t.substr(pos) : 1; + } + long long ans = 0; + int up = min(lim ? t[pos] - '0' : 9, limit); + for (int i = 0; i <= up; ++i) { + ans += dfs(pos + 1, lim && i == (t[pos] - '0')); + } + if (!lim) { + f[pos] = ans; + } + return ans; + }; + + long long a = dfs(0, true); + t = to_string(finish); + memset(f, -1, sizeof(f)); + long long b = dfs(0, true); + return b - a; + } +}; +``` + +### **Go** + +```go +func numberOfPowerfulInt(start, finish int64, limit int, s string) int64 { + t := strconv.FormatInt(start-1, 10) + f := make([]int64, 20) + for i := range f { + f[i] = -1 + } + + var dfs func(int, bool) int64 + dfs = func(pos int, lim bool) int64 { + if len(t) < len(s) { + return 0 + } + if !lim && f[pos] != -1 { + return f[pos] + } + if len(t)-pos == len(s) { + if lim { + if s <= t[pos:] { + return 1 + } + return 0 + } + return 1 + } + + ans := int64(0) + up := 9 + if lim { + up = int(t[pos] - '0') + } + up = min(up, limit) + for i := 0; i <= up; i++ { + ans += dfs(pos+1, lim && i == int(t[pos]-'0')) + } + if !lim { + f[pos] = ans + } + return ans + } + + a := dfs(0, true) + t = strconv.FormatInt(finish, 10) + for i := range f { + f[i] = -1 + } + b := dfs(0, true) + return b - a +} +``` + +### **TypeScript** + +```ts +function numberOfPowerfulInt(start: number, finish: number, limit: number, s: string): number { + let t: string = (start - 1).toString(); + let f: number[] = Array(20).fill(-1); + + const dfs = (pos: number, lim: boolean): number => { + if (t.length < s.length) { + return 0; + } + if (!lim && f[pos] !== -1) { + return f[pos]; + } + if (t.length - pos === s.length) { + if (lim) { + return s <= t.substring(pos) ? 1 : 0; + } + return 1; + } + + let ans: number = 0; + const up: number = Math.min(lim ? +t[pos] : 9, limit); + for (let i = 0; i <= up; i++) { + ans += dfs(pos + 1, lim && i === +t[pos]); + } + + if (!lim) { + f[pos] = ans; + } + return ans; + }; + + const a: number = dfs(0, true); + t = finish.toString(); + f = Array(20).fill(-1); + const b: number = dfs(0, true); + + return b - a; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10034.Count the Number of Powerful Integers/README_EN.md b/solution/10000-10099/10034.Count the Number of Powerful Integers/README_EN.md new file mode 100644 index 0000000000000..6e8f5d755925b --- /dev/null +++ b/solution/10000-10099/10034.Count the Number of Powerful Integers/README_EN.md @@ -0,0 +1,269 @@ +# [10034. Count the Number of Powerful Integers](https://leetcode.com/problems/count-the-number-of-powerful-integers) + +[中文文档](/solution/10000-10099/10034.Count%20the%20Number%20of%20Powerful%20Integers/README.md) + +## Description + +

You are given three integers start, finish, and limit. You are also given a 0-indexed string s representing a positive integer.

+ +

A positive integer x is called powerful if it ends with s (in other words, s is a suffix of x) and each digit in x is at most limit.

+ +

Return the total number of powerful integers in the range [start..finish].

+ +

A string x is a suffix of a string y if and only if x is a substring of y that starts from some index (including 0) in y and extends to the index y.length - 1. For example, 25 is a suffix of 5125 whereas 512 is not.

+ +

 

+

Example 1:

+ +
+Input: start = 1, finish = 6000, limit = 4, s = "124"
+Output: 5
+Explanation: The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.
+It can be shown that there are only 5 powerful integers in this range.
+
+ +

Example 2:

+ +
+Input: start = 15, finish = 215, limit = 6, s = "10"
+Output: 2
+Explanation: The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix.
+It can be shown that there are only 2 powerful integers in this range.
+
+ +

Example 3:

+ +
+Input: start = 1000, finish = 2000, limit = 4, s = "3000"
+Output: 0
+Explanation: All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= start <= finish <= 1015
  • +
  • 1 <= limit <= 9
  • +
  • 1 <= s.length <= floor(log10(finish)) + 1
  • +
  • s only consists of numeric digits which are at most limit.
  • +
  • s does not have leading zeros.
  • +
+ +## Solutions + + + +### **Python3** + +```python +class Solution: + def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int: + @cache + def dfs(pos: int, lim: int): + if len(t) < n: + return 0 + if len(t) - pos == n: + return int(s <= t[pos:]) if lim else 1 + up = min(int(t[pos]) if lim else 9, limit) + ans = 0 + for i in range(up + 1): + ans += dfs(pos + 1, lim and i == int(t[pos])) + return ans + + n = len(s) + t = str(start - 1) + a = dfs(0, True) + dfs.cache_clear() + t = str(finish) + b = dfs(0, True) + return b - a +``` + +### **Java** + +```java +class Solution { + private String s; + private String t; + private Long[] f; + private int limit; + + public long numberOfPowerfulInt(long start, long finish, int limit, String s) { + this.s = s; + this.limit = limit; + t = String.valueOf(start - 1); + f = new Long[20]; + long a = dfs(0, true); + t = String.valueOf(finish); + f = new Long[20]; + long b = dfs(0, true); + return b - a; + } + + private long dfs(int pos, boolean lim) { + if (t.length() < s.length()) { + return 0; + } + if (!lim && f[pos] != null) { + return f[pos]; + } + if (t.length() - pos == s.length()) { + return lim ? (s.compareTo(t.substring(pos)) <= 0 ? 1 : 0) : 1; + } + int up = lim ? t.charAt(pos) - '0' : 9; + up = Math.min(up, limit); + long ans = 0; + for (int i = 0; i <= up; ++i) { + ans += dfs(pos + 1, lim && i == (t.charAt(pos) - '0')); + } + if (!lim) { + f[pos] = ans; + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long numberOfPowerfulInt(long long start, long long finish, int limit, string s) { + string t = to_string(start - 1); + long long f[20]; + memset(f, -1, sizeof(f)); + + function dfs = [&](int pos, bool lim) -> long long { + if (t.size() < s.size()) { + return 0; + } + if (!lim && f[pos] != -1) { + return f[pos]; + } + if (t.size() - pos == s.size()) { + return lim ? s <= t.substr(pos) : 1; + } + long long ans = 0; + int up = min(lim ? t[pos] - '0' : 9, limit); + for (int i = 0; i <= up; ++i) { + ans += dfs(pos + 1, lim && i == (t[pos] - '0')); + } + if (!lim) { + f[pos] = ans; + } + return ans; + }; + + long long a = dfs(0, true); + t = to_string(finish); + memset(f, -1, sizeof(f)); + long long b = dfs(0, true); + return b - a; + } +}; +``` + +### **Go** + +```go +func numberOfPowerfulInt(start, finish int64, limit int, s string) int64 { + t := strconv.FormatInt(start-1, 10) + f := make([]int64, 20) + for i := range f { + f[i] = -1 + } + + var dfs func(int, bool) int64 + dfs = func(pos int, lim bool) int64 { + if len(t) < len(s) { + return 0 + } + if !lim && f[pos] != -1 { + return f[pos] + } + if len(t)-pos == len(s) { + if lim { + if s <= t[pos:] { + return 1 + } + return 0 + } + return 1 + } + + ans := int64(0) + up := 9 + if lim { + up = int(t[pos] - '0') + } + up = min(up, limit) + for i := 0; i <= up; i++ { + ans += dfs(pos+1, lim && i == int(t[pos]-'0')) + } + if !lim { + f[pos] = ans + } + return ans + } + + a := dfs(0, true) + t = strconv.FormatInt(finish, 10) + for i := range f { + f[i] = -1 + } + b := dfs(0, true) + return b - a +} +``` + +### **TypeScript** + +```ts +function numberOfPowerfulInt(start: number, finish: number, limit: number, s: string): number { + let t: string = (start - 1).toString(); + let f: number[] = Array(20).fill(-1); + + const dfs = (pos: number, lim: boolean): number => { + if (t.length < s.length) { + return 0; + } + if (!lim && f[pos] !== -1) { + return f[pos]; + } + if (t.length - pos === s.length) { + if (lim) { + return s <= t.substring(pos) ? 1 : 0; + } + return 1; + } + + let ans: number = 0; + const up: number = Math.min(lim ? +t[pos] : 9, limit); + for (let i = 0; i <= up; i++) { + ans += dfs(pos + 1, lim && i === +t[pos]); + } + + if (!lim) { + f[pos] = ans; + } + return ans; + }; + + const a: number = dfs(0, true); + t = finish.toString(); + f = Array(20).fill(-1); + const b: number = dfs(0, true); + + return b - a; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.cpp b/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.cpp new file mode 100644 index 0000000000000..dc348ec436cb0 --- /dev/null +++ b/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + long long numberOfPowerfulInt(long long start, long long finish, int limit, string s) { + string t = to_string(start - 1); + long long f[20]; + memset(f, -1, sizeof(f)); + + function dfs = [&](int pos, bool lim) -> long long { + if (t.size() < s.size()) { + return 0; + } + if (!lim && f[pos] != -1) { + return f[pos]; + } + if (t.size() - pos == s.size()) { + return lim ? s <= t.substr(pos) : 1; + } + long long ans = 0; + int up = min(lim ? t[pos] - '0' : 9, limit); + for (int i = 0; i <= up; ++i) { + ans += dfs(pos + 1, lim && i == (t[pos] - '0')); + } + if (!lim) { + f[pos] = ans; + } + return ans; + }; + + long long a = dfs(0, true); + t = to_string(finish); + memset(f, -1, sizeof(f)); + long long b = dfs(0, true); + return b - a; + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.go b/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.go new file mode 100644 index 0000000000000..b7a7026ab8d2b --- /dev/null +++ b/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.go @@ -0,0 +1,48 @@ +func numberOfPowerfulInt(start, finish int64, limit int, s string) int64 { + t := strconv.FormatInt(start-1, 10) + f := make([]int64, 20) + for i := range f { + f[i] = -1 + } + + var dfs func(int, bool) int64 + dfs = func(pos int, lim bool) int64 { + if len(t) < len(s) { + return 0 + } + if !lim && f[pos] != -1 { + return f[pos] + } + if len(t)-pos == len(s) { + if lim { + if s <= t[pos:] { + return 1 + } + return 0 + } + return 1 + } + + ans := int64(0) + up := 9 + if lim { + up = int(t[pos] - '0') + } + up = min(up, limit) + for i := 0; i <= up; i++ { + ans += dfs(pos+1, lim && i == int(t[pos]-'0')) + } + if !lim { + f[pos] = ans + } + return ans + } + + a := dfs(0, true) + t = strconv.FormatInt(finish, 10) + for i := range f { + f[i] = -1 + } + b := dfs(0, true) + return b - a +} \ No newline at end of file diff --git a/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.java b/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.java new file mode 100644 index 0000000000000..0ba4199184200 --- /dev/null +++ b/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.java @@ -0,0 +1,40 @@ +class Solution { + private String s; + private String t; + private Long[] f; + private int limit; + + public long numberOfPowerfulInt(long start, long finish, int limit, String s) { + this.s = s; + this.limit = limit; + t = String.valueOf(start - 1); + f = new Long[20]; + long a = dfs(0, true); + t = String.valueOf(finish); + f = new Long[20]; + long b = dfs(0, true); + return b - a; + } + + private long dfs(int pos, boolean lim) { + if (t.length() < s.length()) { + return 0; + } + if (!lim && f[pos] != null) { + return f[pos]; + } + if (t.length() - pos == s.length()) { + return lim ? (s.compareTo(t.substring(pos)) <= 0 ? 1 : 0) : 1; + } + int up = lim ? t.charAt(pos) - '0' : 9; + up = Math.min(up, limit); + long ans = 0; + for (int i = 0; i <= up; ++i) { + ans += dfs(pos + 1, lim && i == (t.charAt(pos) - '0')); + } + if (!lim) { + f[pos] = ans; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.py b/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.py new file mode 100644 index 0000000000000..6929287609761 --- /dev/null +++ b/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.py @@ -0,0 +1,21 @@ +class Solution: + def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int: + @cache + def dfs(pos: int, lim: int): + if len(t) < n: + return 0 + if len(t) - pos == n: + return int(s <= t[pos:]) if lim else 1 + up = min(int(t[pos]) if lim else 9, limit) + ans = 0 + for i in range(up + 1): + ans += dfs(pos + 1, lim and i == int(t[pos])) + return ans + + n = len(s) + t = str(start - 1) + a = dfs(0, True) + dfs.cache_clear() + t = str(finish) + b = dfs(0, True) + return b - a diff --git a/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.ts b/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.ts new file mode 100644 index 0000000000000..d33d8a450a81f --- /dev/null +++ b/solution/10000-10099/10034.Count the Number of Powerful Integers/Solution.ts @@ -0,0 +1,37 @@ +function numberOfPowerfulInt(start: number, finish: number, limit: number, s: string): number { + let t: string = (start - 1).toString(); + let f: number[] = Array(20).fill(-1); + + const dfs = (pos: number, lim: boolean): number => { + if (t.length < s.length) { + return 0; + } + if (!lim && f[pos] !== -1) { + return f[pos]; + } + if (t.length - pos === s.length) { + if (lim) { + return s <= t.substring(pos) ? 1 : 0; + } + return 1; + } + + let ans: number = 0; + const up: number = Math.min(lim ? +t[pos] : 9, limit); + for (let i = 0; i <= up; i++) { + ans += dfs(pos + 1, lim && i === +t[pos]); + } + + if (!lim) { + f[pos] = ans; + } + return ans; + }; + + const a: number = dfs(0, true); + t = finish.toString(); + f = Array(20).fill(-1); + const b: number = dfs(0, true); + + return b - a; +} diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README.md b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README.md new file mode 100644 index 0000000000000..d0469bce81885 --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README.md @@ -0,0 +1,158 @@ +# [10035. 对角线最长的矩形的面积](https://leetcode.cn/problems/maximum-area-of-longest-diagonal-rectangle) + +[English Version](/solution/10000-10099/10035.Maximum%20Area%20of%20Longest%20Diagonal%20Rectangle/README_EN.md) + +## 题目描述 + + + +

给你一个下标从 0 开始的二维整数数组 dimensions

+ +

对于所有下标 i0 <= i < dimensions.length),dimensions[i][0] 表示矩形 i 的长度,而 dimensions[i][1] 表示矩形 i 的宽度。

+ +

返回对角线最 的矩形的 面积 。如果存在多个对角线长度相同的矩形,返回面积最的矩形的面积。

+ +

 

+ +

示例 1:

+ +
+输入:dimensions = [[9,3],[8,6]]
+输出:48
+解释:
+下标 = 0,长度 = 9,宽度 = 3。对角线长度 = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487。
+下标 = 1,长度 = 8,宽度 = 6。对角线长度 = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10。
+因此,下标为 1 的矩形对角线更长,所以返回面积 = 8 * 6 = 48。
+
+ +

示例 2:

+ +
+输入:dimensions = [[3,4],[4,3]]
+输出:12
+解释:两个矩形的对角线长度相同,为 5,所以最大面积 = 12。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= dimensions.length <= 100
  • +
  • dimensions[i].length == 2
  • +
  • 1 <= dimensions[i][0], dimensions[i][1] <= 100
  • +
+ +## 解法 + + + + + +### **Python3** + + + +```python +class Solution: + def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int: + ans = mx = 0 + for l, w in dimensions: + t = l**2 + w**2 + if mx < t: + mx = t + ans = l * w + elif mx == t: + ans = max(ans, l * w) + return ans +``` + +### **Java** + + + +```java +class Solution { + public int areaOfMaxDiagonal(int[][] dimensions) { + int ans = 0, mx = 0; + for (var d : dimensions) { + int l = d[0], w = d[1]; + int t = l * l + w * w; + if (mx < t) { + mx = t; + ans = l * w; + } else if (mx == t) { + ans = Math.max(ans, l * w); + } + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int areaOfMaxDiagonal(vector>& dimensions) { + int ans = 0, mx = 0; + for (auto& d : dimensions) { + int l = d[0], w = d[1]; + int t = l * l + w * w; + if (mx < t) { + mx = t; + ans = l * w; + } else if (mx == t) { + ans = max(ans, l * w); + } + } + return ans; + } +}; +``` + +### **Go** + +```go +func areaOfMaxDiagonal(dimensions [][]int) (ans int) { + mx := 0 + for _, d := range dimensions { + l, w := d[0], d[1] + t := l*l + w*w + if mx < t { + mx = t + ans = l * w + } else if mx == t { + ans = max(ans, l*w) + } + } + return +} +``` + +### **TypeScript** + +```ts +function areaOfMaxDiagonal(dimensions: number[][]): number { + let [ans, mx] = [0, 0]; + for (const [l, w] of dimensions) { + const t = l * l + w * w; + if (mx < t) { + mx = t; + ans = l * w; + } else if (mx === t) { + ans = Math.max(ans, l * w); + } + } + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README_EN.md b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README_EN.md new file mode 100644 index 0000000000000..106c7aa8f4d36 --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README_EN.md @@ -0,0 +1,148 @@ +# [10035. Maximum Area of Longest Diagonal Rectangle](https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle) + +[中文文档](/solution/10000-10099/10035.Maximum%20Area%20of%20Longest%20Diagonal%20Rectangle/README.md) + +## Description + +

You are given a 2D 0-indexed integer array dimensions.

+ +

For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i.

+ +

Return the area of the rectangle having the longest diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the maximum area.

+ +

 

+

Example 1:

+ +
+Input: dimensions = [[9,3],[8,6]]
+Output: 48
+Explanation: 
+For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
+For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
+So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
+
+ +

Example 2:

+ +
+Input: dimensions = [[3,4],[4,3]]
+Output: 12
+Explanation: Length of diagonal is the same for both which is 5, so maximum area = 12.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= dimensions.length <= 100
  • +
  • dimensions[i].length == 2
  • +
  • 1 <= dimensions[i][0], dimensions[i][1] <= 100
  • +
+ +## Solutions + + + +### **Python3** + +```python +class Solution: + def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int: + ans = mx = 0 + for l, w in dimensions: + t = l**2 + w**2 + if mx < t: + mx = t + ans = l * w + elif mx == t: + ans = max(ans, l * w) + return ans +``` + +### **Java** + +```java +class Solution { + public int areaOfMaxDiagonal(int[][] dimensions) { + int ans = 0, mx = 0; + for (var d : dimensions) { + int l = d[0], w = d[1]; + int t = l * l + w * w; + if (mx < t) { + mx = t; + ans = l * w; + } else if (mx == t) { + ans = Math.max(ans, l * w); + } + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int areaOfMaxDiagonal(vector>& dimensions) { + int ans = 0, mx = 0; + for (auto& d : dimensions) { + int l = d[0], w = d[1]; + int t = l * l + w * w; + if (mx < t) { + mx = t; + ans = l * w; + } else if (mx == t) { + ans = max(ans, l * w); + } + } + return ans; + } +}; +``` + +### **Go** + +```go +func areaOfMaxDiagonal(dimensions [][]int) (ans int) { + mx := 0 + for _, d := range dimensions { + l, w := d[0], d[1] + t := l*l + w*w + if mx < t { + mx = t + ans = l * w + } else if mx == t { + ans = max(ans, l*w) + } + } + return +} +``` + +### **TypeScript** + +```ts +function areaOfMaxDiagonal(dimensions: number[][]): number { + let [ans, mx] = [0, 0]; + for (const [l, w] of dimensions) { + const t = l * l + w * w; + if (mx < t) { + mx = t; + ans = l * w; + } else if (mx === t) { + ans = Math.max(ans, l * w); + } + } + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.cpp b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.cpp new file mode 100644 index 0000000000000..8a42eb8e05399 --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int areaOfMaxDiagonal(vector>& dimensions) { + int ans = 0, mx = 0; + for (auto& d : dimensions) { + int l = d[0], w = d[1]; + int t = l * l + w * w; + if (mx < t) { + mx = t; + ans = l * w; + } else if (mx == t) { + ans = max(ans, l * w); + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.go b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.go new file mode 100644 index 0000000000000..a217d6d0f8548 --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.go @@ -0,0 +1,14 @@ +func areaOfMaxDiagonal(dimensions [][]int) (ans int) { + mx := 0 + for _, d := range dimensions { + l, w := d[0], d[1] + t := l*l + w*w + if mx < t { + mx = t + ans = l * w + } else if mx == t { + ans = max(ans, l*w) + } + } + return +} \ No newline at end of file diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.java b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.java new file mode 100644 index 0000000000000..2692caa29a9dc --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.java @@ -0,0 +1,16 @@ +class Solution { + public int areaOfMaxDiagonal(int[][] dimensions) { + int ans = 0, mx = 0; + for (var d : dimensions) { + int l = d[0], w = d[1]; + int t = l * l + w * w; + if (mx < t) { + mx = t; + ans = l * w; + } else if (mx == t) { + ans = Math.max(ans, l * w); + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.py b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.py new file mode 100644 index 0000000000000..ff0279628b623 --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int: + ans = mx = 0 + for l, w in dimensions: + t = l**2 + w**2 + if mx < t: + mx = t + ans = l * w + elif mx == t: + ans = max(ans, l * w) + return ans diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.ts b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.ts new file mode 100644 index 0000000000000..3c97b1f2cabb1 --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.ts @@ -0,0 +1,13 @@ +function areaOfMaxDiagonal(dimensions: number[][]): number { + let [ans, mx] = [0, 0]; + for (const [l, w] of dimensions) { + const t = l * l + w * w; + if (mx < t) { + mx = t; + ans = l * w; + } else if (mx === t) { + ans = Math.max(ans, l * w); + } + } + return ans; +} diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md new file mode 100644 index 0000000000000..dbabbdf85d957 --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README.md @@ -0,0 +1,100 @@ +# [10036. 捕获黑皇后需要的最少移动次数](https://leetcode.cn/problems/minimum-moves-to-capture-the-queen) + +[English Version](/solution/10000-10099/10036.Minimum%20Moves%20to%20Capture%20The%20Queen/README_EN.md) + +## 题目描述 + + + +

现有一个下标从 0 开始的 8 x 8 棋盘,上面有 3 枚棋子。

+ +

给你 6 个整数 abcdef ,其中:

+ +
    +
  • (a, b) 表示白色车的位置。
  • +
  • (c, d) 表示白色象的位置。
  • +
  • (e, f) 表示黑皇后的位置。
  • +
+ +

假定你只能移动白色棋子,返回捕获黑皇后所需的最少移动次数。

+ +

请注意

+ +
    +
  • 车可以向垂直或水平方向移动任意数量的格子,但不能跳过其他棋子。
  • +
  • 象可以沿对角线方向移动任意数量的格子,但不能跳过其他棋子。
  • +
  • 如果车或象能移向皇后所在的格子,则认为它们可以捕获皇后。
  • +
  • 皇后不能移动。
  • +
+ +

 

+ +

示例 1:

+ +
+输入:a = 1, b = 1, c = 8, d = 8, e = 2, f = 3
+输出:2
+解释:将白色车先移动到 (1, 3) ,然后移动到 (2, 3) 来捕获黑皇后,共需移动 2 次。
+由于起始时没有任何棋子正在攻击黑皇后,要想捕获黑皇后,移动次数不可能少于 2 次。
+
+ +

示例 2:

+ +
+输入:a = 5, b = 3, c = 3, d = 4, e = 5, f = 2
+输出:1
+解释:可以通过以下任一方式移动 1 次捕获黑皇后:
+- 将白色车移动到 (5, 2) 。
+- 将白色象移动到 (5, 2) 。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= a, b, c, d, e, f <= 8
  • +
  • 两枚棋子不会同时出现在同一个格子上。
  • +
+ +## 解法 + + + + + +### **Python3** + + + +```python + +``` + +### **Java** + + + +```java + +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md new file mode 100644 index 0000000000000..8340757c22f7f --- /dev/null +++ b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/README_EN.md @@ -0,0 +1,90 @@ +# [10036. Minimum Moves to Capture The Queen](https://leetcode.com/problems/minimum-moves-to-capture-the-queen) + +[中文文档](/solution/10000-10099/10036.Minimum%20Moves%20to%20Capture%20The%20Queen/README.md) + +## Description + +

There is a 1-indexed 8 x 8 chessboard containing 3 pieces.

+ +

You are given 6 integers a, b, c, d, e, and f where:

+ +
    +
  • (a, b) denotes the position of the white rook.
  • +
  • (c, d) denotes the position of the white bishop.
  • +
  • (e, f) denotes the position of the black queen.
  • +
+ +

Given that you can only move the white pieces, return the minimum number of moves required to capture the black queen.

+ +

Note that:

+ +
    +
  • Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces.
  • +
  • Bishops can move any number of squares diagonally, but cannot jump over other pieces.
  • +
  • A rook or a bishop can capture the queen if it is located in a square that they can move to.
  • +
  • The queen does not move.
  • +
+ +

 

+

Example 1:

+ +
+Input: a = 1, b = 1, c = 8, d = 8, e = 2, f = 3
+Output: 2
+Explanation: We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3).
+It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning.
+
+ +

Example 2:

+ +
+Input: a = 5, b = 3, c = 3, d = 4, e = 5, f = 2
+Output: 1
+Explanation: We can capture the black queen in a single move by doing one of the following: 
+- Move the white rook to (5, 2).
+- Move the white bishop to (5, 2).
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= a, b, c, d, e, f <= 8
  • +
  • No two pieces are on the same square.
  • +
+ +## Solutions + + + +### **Python3** + +```python + +``` + +### **Java** + +```java + +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/images/ex1.png b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/images/ex1.png new file mode 100644 index 0000000000000..7621069b3105d Binary files /dev/null and b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/images/ex1.png differ diff --git a/solution/10000-10099/10036.Minimum Moves to Capture The Queen/images/ex2.png b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/images/ex2.png new file mode 100644 index 0000000000000..d0f43a360a83f Binary files /dev/null and b/solution/10000-10099/10036.Minimum Moves to Capture The Queen/images/ex2.png differ diff --git a/solution/10000-10099/10037.Maximum Size of a Set After Removals/README.md b/solution/10000-10099/10037.Maximum Size of a Set After Removals/README.md new file mode 100644 index 0000000000000..1febb5498b758 --- /dev/null +++ b/solution/10000-10099/10037.Maximum Size of a Set After Removals/README.md @@ -0,0 +1,94 @@ +# [10037. 移除后集合的最多元素数](https://leetcode.cn/problems/maximum-size-of-a-set-after-removals) + +[English Version](/solution/10000-10099/10037.Maximum%20Size%20of%20a%20Set%20After%20Removals/README_EN.md) + +## 题目描述 + + + +

给你两个下标从 0 开始的整数数组 nums1nums2 ,它们的长度都是偶数 n

+ +

你必须从 nums1 中移除 n / 2 个元素,同时从 nums2 中也移除 n / 2 个元素。移除之后,你将 nums1nums2 中剩下的元素插入到集合 s 中。

+ +

返回集合 s可能的 最多 包含多少元素。

+ +

 

+ +

示例 1:

+ +
+输入:nums1 = [1,2,1,2], nums2 = [1,1,1,1]
+输出:2
+解释:从 nums1 和 nums2 中移除两个 1 。移除后,数组变为 nums1 = [2,2] 和 nums2 = [1,1] 。因此,s = {1,2} 。
+可以证明,在移除之后,集合 s 最多可以包含 2 个元素。
+
+ +

示例 2:

+ +
+输入:nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
+输出:5
+解释:从 nums1 中移除 2、3 和 6 ,同时从 nums2 中移除两个 3 和一个 2 。移除后,数组变为 nums1 = [1,4,5] 和 nums2 = [2,3,2] 。因此,s = {1,2,3,4,5} 。
+可以证明,在移除之后,集合 s 最多可以包含 5 个元素。 
+
+ +

示例 3:

+ +
+输入:nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
+输出:6
+解释:从 nums1 中移除 1、2 和 3 ,同时从 nums2 中移除 4、5 和 6 。移除后,数组变为 nums1 = [1,2,3] 和 nums2 = [4,5,6] 。因此,s = {1,2,3,4,5,6} 。
+可以证明,在移除之后,集合 s 最多可以包含 6 个元素。 
+ +

 

+ +

提示:

+ +
    +
  • n == nums1.length == nums2.length
  • +
  • 1 <= n <= 2 * 104
  • +
  • n是偶数。
  • +
  • 1 <= nums1[i], nums2[i] <= 109
  • +
+ +## 解法 + + + + + +### **Python3** + + + +```python + +``` + +### **Java** + + + +```java + +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10037.Maximum Size of a Set After Removals/README_EN.md b/solution/10000-10099/10037.Maximum Size of a Set After Removals/README_EN.md new file mode 100644 index 0000000000000..48e6ae02a235e --- /dev/null +++ b/solution/10000-10099/10037.Maximum Size of a Set After Removals/README_EN.md @@ -0,0 +1,85 @@ +# [10037. Maximum Size of a Set After Removals](https://leetcode.com/problems/maximum-size-of-a-set-after-removals) + +[中文文档](/solution/10000-10099/10037.Maximum%20Size%20of%20a%20Set%20After%20Removals/README.md) + +## Description + +

You are given two 0-indexed integer arrays nums1 and nums2 of even length n.

+ +

You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals, you insert the remaining elements of nums1 and nums2 into a set s.

+ +

Return the maximum possible size of the set s.

+ +

 

+

Example 1:

+ +
+Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]
+Output: 2
+Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
+It can be shown that 2 is the maximum possible size of the set s after the removals.
+
+ +

Example 2:

+ +
+Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
+Output: 5
+Explanation: We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
+It can be shown that 5 is the maximum possible size of the set s after the removals.
+
+ +

Example 3:

+ +
+Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
+Output: 6
+Explanation: We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
+It can be shown that 6 is the maximum possible size of the set s after the removals.
+
+ +

 

+

Constraints:

+ +
    +
  • n == nums1.length == nums2.length
  • +
  • 1 <= n <= 2 * 104
  • +
  • n is even.
  • +
  • 1 <= nums1[i], nums2[i] <= 109
  • +
+ +## Solutions + + + +### **Python3** + +```python + +``` + +### **Java** + +```java + +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10038.Maximize the Number of Partitions After Operations/README.md b/solution/10000-10099/10038.Maximize the Number of Partitions After Operations/README.md new file mode 100644 index 0000000000000..1f7e21e2497c4 --- /dev/null +++ b/solution/10000-10099/10038.Maximize the Number of Partitions After Operations/README.md @@ -0,0 +1,122 @@ +# [10038. 执行操作后的最大分割数量](https://leetcode.cn/problems/maximize-the-number-of-partitions-after-operations) + +[English Version](/solution/10000-10099/10038.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README_EN.md) + +## 题目描述 + + + +

给你一个下标从 0 开始的字符串 s 和一个整数 k

+ +

你需要执行以下分割操作,直到字符串 变为 

+ +
    +
  • 选择 s 的最长前缀,该前缀最多包含 个 不同 字符。
  • +
  • 删除 这个前缀,并将分割数量加一。如果有剩余字符,它们在 s 中保持原来的顺序。
  • +
+ +

执行操作之 ,你可以将 s 中 至多一处 下标的对应字符更改为另一个小写英文字母。

+ +

在最优选择情形下改变至多一处下标对应字符后,用整数表示并返回操作结束时得到的最大分割数量。

+ +

 

+ +

示例 1:

+ +
+输入:s = "accca", k = 2
+输出:3
+解释:在此示例中,为了最大化得到的分割数量,可以将 s[2] 改为 'b'。
+s 变为 "acbca"。
+按照以下方式执行操作,直到 s 变为空:
+- 选择最长且至多包含 2 个不同字符的前缀,"acbca"。
+- 删除该前缀,s 变为 "bca"。现在分割数量为 1。
+- 选择最长且至多包含 2 个不同字符的前缀,"bca"。
+- 删除该前缀,s 变为 "a"。现在分割数量为 2。
+- 选择最长且至多包含 2 个不同字符的前缀,"a"。
+- 删除该前缀,s 变为空。现在分割数量为 3。
+因此,答案是 3。
+可以证明,分割数量不可能超过 3。
+ +

示例 2:

+ +
+输入:s = "aabaab", k = 3
+输出:1
+解释:在此示例中,为了最大化得到的分割数量,可以保持 s 不变。
+按照以下方式执行操作,直到 s 变为空: 
+- 选择最长且至多包含 3 个不同字符的前缀,"aabaab"。
+- 删除该前缀,s 变为空。现在分割数量为 1。
+因此,答案是 1。
+可以证明,分割数量不可能超过 1。
+ +

示例 3:

+ +
+输入:s = "xxyz", k = 1
+输出:4
+解释:在此示例中,为了最大化得到的分割数量,可以将 s[1] 改为 'a'。
+s 变为 "xayz"。
+按照以下方式执行操作,直到 s 变为空:
+- 选择最长且至多包含 1 个不同字符的前缀,"xayz"。
+- 删除该前缀,s 变为 "ayz"。现在分割数量为 1。
+- 选择最长且至多包含 1 个不同字符的前缀,"ayz"。
+- 删除该前缀,s 变为 "yz",现在分割数量为 2。
+- 选择最长且至多包含 1 个不同字符的前缀,"yz"。
+- 删除该前缀,s 变为 "z"。现在分割数量为 3。
+- 选择最且至多包含 1 个不同字符的前缀,"z"。
+- 删除该前缀,s 变为空。现在分割数量为 4。
+因此,答案是 4。
+可以证明,分割数量不可能超过 4。
+ +

 

+ +

提示:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s 只包含小写英文字母。
  • +
  • 1 <= k <= 26
  • +
+ +## 解法 + + + + + +### **Python3** + + + +```python + +``` + +### **Java** + + + +```java + +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **...** + +``` + +``` + + diff --git a/solution/10000-10099/10038.Maximize the Number of Partitions After Operations/README_EN.md b/solution/10000-10099/10038.Maximize the Number of Partitions After Operations/README_EN.md new file mode 100644 index 0000000000000..4c356c31570e7 --- /dev/null +++ b/solution/10000-10099/10038.Maximize the Number of Partitions After Operations/README_EN.md @@ -0,0 +1,113 @@ +# [10038. Maximize the Number of Partitions After Operations](https://leetcode.com/problems/maximize-the-number-of-partitions-after-operations) + +[中文文档](/solution/10000-10099/10038.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README.md) + +## Description + +

You are given a 0-indexed string s and an integer k.

+ +

You are to perform the following partitioning operations until s is empty:

+ +
    +
  • Choose the longest prefix of s containing at most k distinct characters.
  • +
  • Delete the prefix from s and increase the number of partitions by one. The remaining characters (if any) in s maintain their initial order.
  • +
+ +

Before the operations, you are allowed to change at most one index in s to another lowercase English letter.

+ +

Return an integer denoting the maximum number of resulting partitions after the operations by optimally choosing at most one index to change.

+

 

+

Example 1:

+ +
+Input: s = "accca", k = 2
+Output: 3
+Explanation: In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'.
+s becomes "acbca".
+The operations can now be performed as follows until s becomes empty:
+- Choose the longest prefix containing at most 2 distinct characters, "acbca".
+- Delete the prefix, and s becomes "bca". The number of partitions is now 1.
+- Choose the longest prefix containing at most 2 distinct characters, "bca".
+- Delete the prefix, and s becomes "a". The number of partitions is now 2.
+- Choose the longest prefix containing at most 2 distinct characters, "a".
+- Delete the prefix, and s becomes empty. The number of partitions is now 3.
+Hence, the answer is 3.
+It can be shown that it is not possible to obtain more than 3 partitions.
+ +

Example 2:

+ +
+Input: s = "aabaab", k = 3
+Output: 1
+Explanation: In this example, to maximize the number of resulting partitions we can leave s as it is.
+The operations can now be performed as follows until s becomes empty: 
+- Choose the longest prefix containing at most 3 distinct characters, "aabaab".
+- Delete the prefix, and s becomes empty. The number of partitions becomes 1. 
+Hence, the answer is 1. 
+It can be shown that it is not possible to obtain more than 1 partition.
+
+ +

Example 3:

+ +
+Input: s = "xxyz", k = 1
+Output: 4
+Explanation: In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'.
+s becomes "xayz".
+The operations can now be performed as follows until s becomes empty:
+- Choose the longest prefix containing at most 1 distinct character, "xayz".
+- Delete the prefix, and s becomes "ayz". The number of partitions is now 1.
+- Choose the longest prefix containing at most 1 distinct character, "ayz".
+- Delete the prefix, and s becomes "yz". The number of partitions is now 2.
+- Choose the longest prefix containing at most 1 distinct character, "yz".
+- Delete the prefix, and s becomes "z". The number of partitions is now 3.
+- Choose the longest prefix containing at most 1 distinct character, "z".
+- Delete the prefix, and s becomes empty. The number of partitions is now 4.
+Hence, the answer is 4.
+It can be shown that it is not possible to obtain more than 4 partitions.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s consists only of lowercase English letters.
  • +
  • 1 <= k <= 26
  • +
+ +## Solutions + + + +### **Python3** + +```python + +``` + +### **Java** + +```java + +``` + +### **C++** + +```cpp + +``` + +### **Go** + +```go + +``` + +### **...** + +``` + +``` + + diff --git a/solution/CONTEST_README.md b/solution/CONTEST_README.md index aca013d3b07de..2c5ee4d9f3b29 100644 --- a/solution/CONTEST_README.md +++ b/solution/CONTEST_README.md @@ -22,6 +22,20 @@ ## 往期竞赛 +#### 第 379 场周赛(2024-01-07 10:30, 90 分钟) 参赛人数 3117 + +- [10035. 对角线最长的矩形的面积](/solution/10000-10099/10035.Maximum%20Area%20of%20Longest%20Diagonal%20Rectangle/README.md) +- [10036. 捕获黑皇后需要的最少移动次数](/solution/10000-10099/10036.Minimum%20Moves%20to%20Capture%20The%20Queen/README.md) +- [10037. 移除后集合的最多元素数](/solution/10000-10099/10037.Maximum%20Size%20of%20a%20Set%20After%20Removals/README.md) +- [10038. 执行操作后的最大分割数量](/solution/10000-10099/10038.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README.md) + +#### 第 121 场双周赛(2024-01-06 22:30, 90 分钟) 参赛人数 2218 + +- [10031. 大于等于顺序前缀和的最小缺失整数](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README.md) +- [10032. 使数组异或和等于 K 的最少操作次数](/solution/10000-10099/10032.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README.md) +- [10033. 使 X 和 Y 相等的最少操作次数](/solution/10000-10099/10033.Minimum%20Number%20of%20Operations%20to%20Make%20X%20and%20Y%20Equal/README.md) +- [10034. 统计强大整数的数目](/solution/10000-10099/10034.Count%20the%20Number%20of%20Powerful%20Integers/README.md) + #### 第 378 场周赛(2023-12-31 10:30, 90 分钟) 参赛人数 2747 - [2980. 检查按位或是否存在尾随零](/solution/2900-2999/2980.Check%20if%20Bitwise%20OR%20Has%20Trailing%20Zeros/README.md) diff --git a/solution/CONTEST_README_EN.md b/solution/CONTEST_README_EN.md index b52182a8992a5..d5474e0d4503f 100644 --- a/solution/CONTEST_README_EN.md +++ b/solution/CONTEST_README_EN.md @@ -25,6 +25,20 @@ Get your rating changes right after the completion of LeetCode contests, https:/ ## Past Contests +#### Weekly Contest 379 + +- [10035. Maximum Area of Longest Diagonal Rectangle](/solution/10000-10099/10035.Maximum%20Area%20of%20Longest%20Diagonal%20Rectangle/README_EN.md) +- [10036. Minimum Moves to Capture The Queen](/solution/10000-10099/10036.Minimum%20Moves%20to%20Capture%20The%20Queen/README_EN.md) +- [10037. Maximum Size of a Set After Removals](/solution/10000-10099/10037.Maximum%20Size%20of%20a%20Set%20After%20Removals/README_EN.md) +- [10038. Maximize the Number of Partitions After Operations](/solution/10000-10099/10038.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README_EN.md) + +#### Biweekly Contest 121 + +- [10031. Smallest Missing Integer Greater Than Sequential Prefix Sum](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README_EN.md) +- [10032. Minimum Number of Operations to Make Array XOR Equal to K](/solution/10000-10099/10032.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README_EN.md) +- [10033. Minimum Number of Operations to Make X and Y Equal](/solution/10000-10099/10033.Minimum%20Number%20of%20Operations%20to%20Make%20X%20and%20Y%20Equal/README_EN.md) +- [10034. Count the Number of Powerful Integers](/solution/10000-10099/10034.Count%20the%20Number%20of%20Powerful%20Integers/README_EN.md) + #### Weekly Contest 378 - [2980. Check if Bitwise OR Has Trailing Zeros](/solution/2900-2999/2980.Check%20if%20Bitwise%20OR%20Has%20Trailing%20Zeros/README_EN.md) diff --git a/solution/README.md b/solution/README.md index ba104a4c232a2..c8d48881372f0 100644 --- a/solution/README.md +++ b/solution/README.md @@ -1014,6 +1014,14 @@ | 1001 | [网格照明](/solution/1000-1099/1001.Grid%20Illumination/README.md) | `数组`,`哈希表` | 困难 | 第 125 场周赛 | | 1002 | [查找共用字符](/solution/1000-1099/1002.Find%20Common%20Characters/README.md) | `数组`,`哈希表`,`字符串` | 简单 | 第 126 场周赛 | | 1003 | [检查替换后的词是否有效](/solution/1000-1099/1003.Check%20If%20Word%20Is%20Valid%20After%20Substitutions/README.md) | `栈`,`字符串` | 中等 | 第 126 场周赛 | +| 10031 | [大于等于顺序前缀和的最小缺失整数](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README.md) | | 简单 | 第 121 场双周赛 | +| 10032 | [使数组异或和等于 K 的最少操作次数](/solution/10000-10099/10032.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README.md) | | 中等 | 第 121 场双周赛 | +| 10033 | [使 X 和 Y 相等的最少操作次数](/solution/10000-10099/10033.Minimum%20Number%20of%20Operations%20to%20Make%20X%20and%20Y%20Equal/README.md) | | 中等 | 第 121 场双周赛 | +| 10034 | [统计强大整数的数目](/solution/10000-10099/10034.Count%20the%20Number%20of%20Powerful%20Integers/README.md) | | 困难 | 第 121 场双周赛 | +| 10035 | [对角线最长的矩形的面积](/solution/10000-10099/10035.Maximum%20Area%20of%20Longest%20Diagonal%20Rectangle/README.md) | | 简单 | 第 379 场周赛 | +| 10036 | [捕获黑皇后需要的最少移动次数](/solution/10000-10099/10036.Minimum%20Moves%20to%20Capture%20The%20Queen/README.md) | | 中等 | 第 379 场周赛 | +| 10037 | [移除后集合的最多元素数](/solution/10000-10099/10037.Maximum%20Size%20of%20a%20Set%20After%20Removals/README.md) | | 中等 | 第 379 场周赛 | +| 10038 | [执行操作后的最大分割数量](/solution/10000-10099/10038.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README.md) | | 困难 | 第 379 场周赛 | | 1004 | [最大连续1的个数 III](/solution/1000-1099/1004.Max%20Consecutive%20Ones%20III/README.md) | `数组`,`二分查找`,`前缀和`,`滑动窗口` | 中等 | 第 126 场周赛 | | 1005 | [K 次取反后最大化的数组和](/solution/1000-1099/1005.Maximize%20Sum%20Of%20Array%20After%20K%20Negations/README.md) | `贪心`,`数组`,`排序` | 简单 | 第 127 场周赛 | | 1006 | [笨阶乘](/solution/1000-1099/1006.Clumsy%20Factorial/README.md) | `栈`,`数学`,`模拟` | 中等 | 第 127 场周赛 | diff --git a/solution/README_EN.md b/solution/README_EN.md index 446286e79191e..1b50bfa70d655 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -1012,6 +1012,14 @@ Press Control + F(or Command + F on | 1001 | [Grid Illumination](/solution/1000-1099/1001.Grid%20Illumination/README_EN.md) | `Array`,`Hash Table` | Hard | Weekly Contest 125 | | 1002 | [Find Common Characters](/solution/1000-1099/1002.Find%20Common%20Characters/README_EN.md) | `Array`,`Hash Table`,`String` | Easy | Weekly Contest 126 | | 1003 | [Check If Word Is Valid After Substitutions](/solution/1000-1099/1003.Check%20If%20Word%20Is%20Valid%20After%20Substitutions/README_EN.md) | `Stack`,`String` | Medium | Weekly Contest 126 | +| 10031 | [Smallest Missing Integer Greater Than Sequential Prefix Sum](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README_EN.md) | | Easy | Biweekly Contest 121 | +| 10032 | [Minimum Number of Operations to Make Array XOR Equal to K](/solution/10000-10099/10032.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README_EN.md) | | Medium | Biweekly Contest 121 | +| 10033 | [Minimum Number of Operations to Make X and Y Equal](/solution/10000-10099/10033.Minimum%20Number%20of%20Operations%20to%20Make%20X%20and%20Y%20Equal/README_EN.md) | | Medium | Biweekly Contest 121 | +| 10034 | [Count the Number of Powerful Integers](/solution/10000-10099/10034.Count%20the%20Number%20of%20Powerful%20Integers/README_EN.md) | | Hard | Biweekly Contest 121 | +| 10035 | [Maximum Area of Longest Diagonal Rectangle](/solution/10000-10099/10035.Maximum%20Area%20of%20Longest%20Diagonal%20Rectangle/README_EN.md) | | Easy | Weekly Contest 379 | +| 10036 | [Minimum Moves to Capture The Queen](/solution/10000-10099/10036.Minimum%20Moves%20to%20Capture%20The%20Queen/README_EN.md) | | Medium | Weekly Contest 379 | +| 10037 | [Maximum Size of a Set After Removals](/solution/10000-10099/10037.Maximum%20Size%20of%20a%20Set%20After%20Removals/README_EN.md) | | Medium | Weekly Contest 379 | +| 10038 | [Maximize the Number of Partitions After Operations](/solution/10000-10099/10038.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README_EN.md) | | Hard | Weekly Contest 379 | | 1004 | [Max Consecutive Ones III](/solution/1000-1099/1004.Max%20Consecutive%20Ones%20III/README_EN.md) | `Array`,`Binary Search`,`Prefix Sum`,`Sliding Window` | Medium | Weekly Contest 126 | | 1005 | [Maximize Sum Of Array After K Negations](/solution/1000-1099/1005.Maximize%20Sum%20Of%20Array%20After%20K%20Negations/README_EN.md) | `Greedy`,`Array`,`Sorting` | Easy | Weekly Contest 127 | | 1006 | [Clumsy Factorial](/solution/1000-1099/1006.Clumsy%20Factorial/README_EN.md) | `Stack`,`Math`,`Simulation` | Medium | Weekly Contest 127 | diff --git a/solution/summary.md b/solution/summary.md index 39ee62e053b44..42bc814454b43 100644 --- a/solution/summary.md +++ b/solution/summary.md @@ -1120,6 +1120,16 @@ - [1098.小众书籍](/solution/1000-1099/1098.Unpopular%20Books/README.md) - [1099.小于 K 的两数之和](/solution/1000-1099/1099.Two%20Sum%20Less%20Than%20K/README.md) +- 10000-10099 + - [10031检查替换后的词是否有效](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README.md) + - [10032检查替换后的词是否有效](/solution/10000-10099/10032.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README.md) + - [10033检查替换后的词是否有效](/solution/10000-10099/10033.Minimum%20Number%20of%20Operations%20to%20Make%20X%20and%20Y%20Equal/README.md) + - [10034检查替换后的词是否有效](/solution/10000-10099/10034.Count%20the%20Number%20of%20Powerful%20Integers/README.md) + - [10035检查替换后的词是否有效](/solution/10000-10099/10035.Maximum%20Area%20of%20Longest%20Diagonal%20Rectangle/README.md) + - [10036检查替换后的词是否有效](/solution/10000-10099/10036.Minimum%20Moves%20to%20Capture%20The%20Queen/README.md) + - [10037检查替换后的词是否有效](/solution/10000-10099/10037.Maximum%20Size%20of%20a%20Set%20After%20Removals/README.md) + - [10038检查替换后的词是否有效](/solution/10000-10099/10038.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README.md) + - 1100-1199 - [1100.长度为 K 的无重复字符子串](/solution/1100-1199/1100.Find%20K-Length%20Substrings%20With%20No%20Repeated%20Characters/README.md) - [1101.彼此熟识的最早时间](/solution/1100-1199/1101.The%20Earliest%20Moment%20When%20Everyone%20Become%20Friends/README.md) diff --git a/solution/summary_en.md b/solution/summary_en.md index cdfd81174d895..543e358aa639e 100644 --- a/solution/summary_en.md +++ b/solution/summary_en.md @@ -1120,6 +1120,16 @@ - [1098.Unpopular Books](/solution/1000-1099/1098.Unpopular%20Books/README_EN.md) - [1099.Two Sum Less Than K](/solution/1000-1099/1099.Two%20Sum%20Less%20Than%20K/README_EN.md) +- 10000-10099 + - [10031.Smallest Missing Integer Greater Than Sequential Prefix Sum](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README_EN.md) + - [10032.Minimum Number of Operations to Make Array XOR Equal to K](/solution/10000-10099/10032.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README_EN.md) + - [10033.Minimum Number of Operations to Make X and Y Equal](/solution/10000-10099/10033.Minimum%20Number%20of%20Operations%20to%20Make%20X%20and%20Y%20Equal/README_EN.md) + - [10034.Count the Number of Powerful Integers](/solution/10000-10099/10034.Count%20the%20Number%20of%20Powerful%20Integers/README_EN.md) + - [10035.Maximum Area of Longest Diagonal Rectangle](/solution/10000-10099/10035.Maximum%20Area%20of%20Longest%20Diagonal%20Rectangle/README_EN.md) + - [10036.Minimum Moves to Capture The Queen](/solution/10000-10099/10036.Minimum%20Moves%20to%20Capture%20The%20Queen/README_EN.md) + - [10037.Maximum Size of a Set After Removals](/solution/10000-10099/10037.Maximum%20Size%20of%20a%20Set%20After%20Removals/README_EN.md) + - [10038.Maximize the Number of Partitions After Operations](/solution/10000-10099/10038.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README_EN.md) + - 1100-1199 - [1100.Find K-Length Substrings With No Repeated Characters](/solution/1100-1199/1100.Find%20K-Length%20Substrings%20With%20No%20Repeated%20Characters/README_EN.md) - [1101.The Earliest Moment When Everyone Become Friends](/solution/1100-1199/1101.The%20Earliest%20Moment%20When%20Everyone%20Become%20Friends/README_EN.md)