Skip to content

Commit c374589

Browse files
authored
feat: add solution description to lc problems: No.2533~2541 (doocs#1938)
* No.2533.Number of Good Binary Strings * No.2535.Difference Between Element Sum and Digit Sum of an Array * No.2537.Count the Number of Good Subarrays * No.2538.Difference Between Maximum and Minimum Price Sum * No.2539.Count the Number of Good Subsequences * No.2540.Minimum Common Value * No.2541.Minimum Operations to Make Array Equal II
1 parent 1369d22 commit c374589

File tree

11 files changed

+79
-9
lines changed

11 files changed

+79
-9
lines changed

solution/2500-2599/2533.Number of Good Binary Strings/README_EN.md

+15
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ It can be proven that there is only 1 good string satisfying all conditions.
5555

5656
## Solutions
5757

58+
**Solution 1: Dynamic Programming**
59+
60+
We define $f[i]$ as the number of strings of length $i$ that meet the condition. The state transition equation is:
61+
62+
$$
63+
f[i] = \begin{cases}
64+
1 & i = 0 \\
65+
f[i - oneGroup] + f[i - zeroGroup] & i \geq 1
66+
\end{cases}
67+
$$
68+
69+
The final answer is $f[minLength] + f[minLength + 1] + \cdots + f[maxLength]$.
70+
71+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n=maxLength$.
72+
5873
<!-- tabs:start -->
5974

6075
### **Python3**

solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ nums 的数字和是 1 + 2 + 3 + 4 = 10 。
5656

5757
**方法一:模拟**
5858

59-
遍历数组 `nums`,计算元素和 $a$ 与数字和 $b$,最后返回 $|a - b|$ 即可。
59+
我们遍历数组 $nums$,计算元素和 $a$ 与数字和 $b$,最后返回 $|a - b|$ 即可。
6060

61-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
61+
时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$
6262

6363
<!-- tabs:start -->
6464

solution/2500-2599/2535.Difference Between Element Sum and Digit Sum of an Array/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ The absolute difference between the element sum and digit sum is |10 - 10| = 0.
4848

4949
## Solutions
5050

51+
**Solution 1: Simulation**
52+
53+
We traverse the array $nums$, calculate the sum of elements $a$ and the sum of digits $b$, and finally return $|a - b|$.
54+
55+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**

solution/2500-2599/2537.Count the Number of Good Subarrays/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@
4949

5050
如果一个子数组中包含 $k$ 对相同的元素,那么包含这个子数组的数组一定至少包含 $k$ 对相同的元素。
5151

52-
我们用一个哈希表 `cnt` 统计窗口内数组元素出现的次数,用 `cur` 统计窗口内相同元素的对数,用 $i$ 维护窗口的左端点。
52+
我们用一个哈希表 $cnt$ 统计窗口内数组元素出现的次数,用 $cur$ 统计窗口内相同元素的对数,用 $i$ 维护窗口的左端点。
5353

54-
遍历数组 `nums`,我们将当前元素 $x$ 作为右端点,那么窗口内相同元素的对数将增加 $cnt[x]$,同时将 $x$ 的出现次数加一,即 $cnt[x] \leftarrow cnt[x] + 1$。接下来,我们循环判断移出左端点后窗口内相同元素的对数是否大于等于 $k$,如果大于等于 $k$,那么我们将左端点元素的出现次数减一,即 $cnt[nums[i]] \leftarrow cnt[nums[i]] - 1$,同时将窗口内相同元素的对数减去 $cnt[nums[i]]$,即 $cur \leftarrow cur - cnt[nums[i]]$,同时将左端点右移,即 $i \leftarrow i + 1$。此时窗口左端点以及左侧的所有元素都可以作为当前右端点的左端点,因此答案加上 $i + 1$。
54+
遍历数组 $nums$,我们将当前元素 $x$ 作为右端点,那么窗口内相同元素的对数将增加 $cnt[x]$,同时将 $x$ 的出现次数加一,即 $cnt[x] \leftarrow cnt[x] + 1$。接下来,我们循环判断移出左端点后窗口内相同元素的对数是否大于等于 $k$,如果大于等于 $k$,那么我们将左端点元素的出现次数减一,即 $cnt[nums[i]] \leftarrow cnt[nums[i]] - 1$,同时将窗口内相同元素的对数减去 $cnt[nums[i]]$,即 $cur \leftarrow cur - cnt[nums[i]]$,同时将左端点右移,即 $i \leftarrow i + 1$。此时窗口左端点以及左侧的所有元素都可以作为当前右端点的左端点,因此答案加上 $i + 1$。
5555

5656
最后,我们返回答案即可。
5757

58-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
58+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
5959

6060
<!-- tabs:start -->
6161

solution/2500-2599/2537.Count the Number of Good Subarrays/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@
4141

4242
## Solutions
4343

44+
**Solution 1: Hash Table + Two Pointers**
45+
46+
If a subarray contains $k$ pairs of identical elements, then an array that contains this subarray must contain at least $k$ pairs of identical elements.
47+
48+
We use a hash table $cnt$ to count the number of occurrences of each element in the window, use $cur$ to count the number of pairs of identical elements in the window, and use $i$ to maintain the left endpoint of the window.
49+
50+
We traverse the array $nums$, take the current element $x$ as the right endpoint, then the number of pairs of identical elements in the window will increase by $cnt[x]$, and the occurrence times of $x$ will be increased by one, i.e., $cnt[x] \leftarrow cnt[x] + 1$. Next, we loop to judge whether the number of pairs of identical elements in the window is greater than or equal to $k$ after removing the left endpoint. If it is greater than or equal to $k$, then we decrease the occurrence times of the left endpoint element by one, i.e., $cnt[nums[i]] \leftarrow cnt[nums[i]] - 1$, and decrease the number of pairs of identical elements in the window by $cnt[nums[i]]$, i.e., $cur \leftarrow cur - cnt[nums[i]]$, and move the left endpoint to the right, i.e., $i \leftarrow i + 1$. At this time, all elements to the left of the window left endpoint and the left endpoint itself can be used as the left endpoint of the current right endpoint, so the answer is increased by $i + 1$.
51+
52+
Finally, we return the answer.
53+
54+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
55+
4456
<!-- tabs:start -->
4557

4658
### **Python3**

solution/2500-2599/2538.Difference Between Maximum and Minimum Price Sum/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@
7676
- 前面不去掉端点的最大路径和加上当前节点去掉端点的最大路径和,即 $a + d$;
7777
- 前面去掉端点的最大路径和加上当前节点不去掉端点的最大路径和,即 $b + c$。
7878

79-
我们更新答案的最大值,即 $ans = max(ans, a + d, b + c)$。
79+
我们更新答案的最大值,即 $ans = \max(ans, a + d, b + c)$。
8080

81-
然后更新 $a$ 和 $b$,即 $a = max(a, price[i] + c)$, $b = max(b, price[i] + d)$,最后返回。
81+
然后更新 $a$ 和 $b$,即 $a = \max(a, price[i] + c)$, $b = \max(b, price[i] + d)$,最后返回。
8282

8383
时间复杂度为 $O(n)$,空间复杂度为 $O(n)$。其中 $n$ 为节点个数。
8484

solution/2500-2599/2538.Difference Between Maximum and Minimum Price Sum/README_EN.md

+21
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ The difference between the maximum and minimum price sum is 2. It can be proved
5151

5252
## Solutions
5353

54+
**Soution 1: Tree-shaped DP**
55+
56+
Since the value of each node is a positive integer, the smallest path with node $root$ as the root node is the $root$ node itself. Therefore, the difference between the path with the maximum value sum and the smallest path is equivalent to removing one endpoint of the path.
57+
58+
We design a function $dfs(i, fa)$, which represents the maximum path sum in the subtree with node $i$ as the root node, with and without removing the endpoint. Here, $fa$ represents the parent node of node $i$.
59+
60+
The implementation logic of the function $dfs(i, fa)$ is as follows:
61+
62+
Initialize $a = price[i]$, $b = 0$, indicating that initially there is only one node, the maximum path sum without removing the endpoint is $price[i]$, and the maximum path sum with removing the endpoint is $0$.
63+
64+
For each child node $j$ of node $i$, if $j \ne fa$, then recursively call the function $dfs(j, i)$. Here, it returns the maximum path sum in the subtree with node $j$ as the root node, with and without removing the endpoint, denoted as $c$ and $d$. At this time, there are two cases for the answer:
65+
66+
- The maximum path sum without removing the endpoint plus the maximum path sum of the current node with removing the endpoint, that is, $a + d$;
67+
- The maximum path sum with removing the endpoint plus the maximum path sum of the current node without removing the endpoint, that is, $b + c$.
68+
69+
We update the maximum value of the answer, that is, $ans = \max(ans, a + d, b + c)$.
70+
71+
Then update $a$ and $b$, that is, $a = \max(a, price[i] + c)$, $b = \max(b, price[i] + d)$, and finally return.
72+
73+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes.
74+
5475
<!-- tabs:start -->
5576

5677
### **Python3**

solution/2500-2599/2539.Count the Number of Good Subsequences/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
**方法一:枚举 + 组合计数**
5454

55-
由于题目说的是子序列中字母出现的次数,因此,我们可以先用一个数组 `cnt` 统计字符串 $s$ 中每个字母出现的次数,记最大的次数为 $mx$。
55+
由于题目说的是子序列中字母出现的次数,因此,我们可以先用一个数组 $cnt$ 统计字符串 $s$ 中每个字母出现的次数,记最大的次数为 $mx$。
5656

5757
接下来,我们在 $[1,2..mx]$ 范围内枚举子序列中字母出现的次数 $i$,然后枚举所有出现过的字母,如果该字母 $c$ 的出现次数 $cnt[c]$ 大于等于 $i$,那么我们可以从这 $cnt[c]$ 个相同字母中选择其中 $i$ 个,也可以一个都不选,那么当前字母的可选方案数就是 $comb(cnt[c], i) + 1$,将所有可选方案数相乘,可以得到当前次数的所有子序列次数,将次数减去 $1$ 累加到答案中。
5858

solution/2500-2599/2540.Minimum Common Value/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
遍历两个数组,如果两个指针指向的元素相等,则返回该元素;如果两个指针指向的元素不相等,则将指向较小元素的指针右移一位,直到找到相等的元素或者遍历完数组。
4646

47-
时间复杂度 $O(m + n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是两个数组的长度。
47+
时间复杂度 $O(m + n)$,其中 $m$ 和 $n$ 分别是两个数组的长度。空间复杂度 $O(1)$
4848

4949
<!-- tabs:start -->
5050

solution/2500-2599/2540.Minimum Common Value/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636

3737
## Solutions
3838

39+
**Solution 1: Two Pointers**
40+
41+
Traverse the two arrays. If the elements pointed to by the two pointers are equal, return that element. If the elements pointed to by the two pointers are not equal, move the pointer pointing to the smaller element to the right by one bit until an equal element is found or the array is traversed.
42+
43+
The time complexity is $O(m + n)$, where $m$ and $n$ are the lengths of the two arrays respectively. The space complexity is $O(1)$.
44+
3945
<!-- tabs:start -->
4046

4147
### **Python3**

solution/2500-2599/2541.Minimum Operations to Make Array Equal II/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ One can prove that it is impossible to make arrays equal in fewer operations.</p
4545

4646
## Solutions
4747

48+
**Solution 1: Single Pass**
49+
50+
We use a variable $x$ to record the difference in the number of additions and subtractions, and a variable $ans$ to record the number of operations.
51+
52+
We traverse the array, and for each position $i$, if $k=0$ and $a_i \neq b_i$, then it is impossible to make the two arrays equal, so we return $-1$. Otherwise, if $k \neq 0$, then $a_i - b_i$ must be a multiple of $k$, otherwise it is impossible to make the two arrays equal, so we return $-1$. Next, we update $x$ and $ans$.
53+
54+
Finally, if $x \neq 0$, then it is impossible to make the two arrays equal, so we return $-1$. Otherwise, we return $\frac{ans}{2}$.
55+
56+
The time complexity is $O(n)$, and the space complexity is $O(1)$, where $n$ is the length of the array.
57+
4858
<!-- tabs:start -->
4959

5060
### **Python3**

0 commit comments

Comments
 (0)