Skip to content

Commit 9480261

Browse files
authored
feat: add solutions to lc problems: No.2824~2867 (#2007)
1 parent e256fd3 commit 9480261

File tree

41 files changed

+396
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+396
-1
lines changed

solution/2800-2899/2824.Count Pairs Whose Sum is Less than Target/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less tha
4747

4848
## Solutions
4949

50+
**Solution 1: Sorting + Binary Search**
51+
52+
First, we sort the array $nums$. Then, for each $j$, we use binary search in the range $[0, j)$ to find the first index $i$ that is greater than or equal to $target - nums[j]$. All indices $k$ in the range $[0, i)$ meet the condition, so the answer increases by $i$.
53+
54+
After the traversal, we return the answer.
55+
56+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $nums$.
57+
5058
<!-- tabs:start -->
5159

5260
### **Python3**

solution/2800-2899/2825.Make String a Subsequence Using Cyclic Increments/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ Therefore, false is returned.</pre>
5151

5252
## Solutions
5353

54+
**Solution 1: Two Pointers**
55+
56+
This problem actually requires us to determine whether a string $s$ is a subsequence of another string $t$. However, the characters do not have to match exactly. If two characters are the same, or one character is the next character of the other, they can match.
57+
58+
The time complexity is $O(m + n)$, where $m$ and $n$ are the lengths of the strings $str1$ and $str2$ respectively. The space complexity is $O(1)$.
59+
5460
<!-- tabs:start -->
5561

5662
### **Python3**

solution/2800-2899/2826.Sorting Three Groups/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878

7979
**方法一:动态规划**
8080

81-
我们定义 $f[i][j]$ 表示将前 $i$ 个数变成美丽数组,并且第 $i$ 个数变成 $j+1$ 的最少操作次数。那么答案就是 $min(f[n][0], f[n][1], f[n][2])$。
81+
我们定义 $f[i][j]$ 表示将前 $i$ 个数变成美丽数组,并且第 $i$ 个数变成 $j+1$ 的最少操作次数。那么答案就是 $\min(f[n][0], f[n][1], f[n][2])$。
8282

8383
我们可以枚举第 $i$ 个数变成 $j+1$ 的所有情况,然后取最小值。这里我们可以用滚动数组优化空间复杂度。
8484

solution/2800-2899/2826.Sorting Three Groups/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ After sorting the numbers in each group, group 1 becomes empty, group 2 becomes
7070

7171
## Solutions
7272

73+
**Solution 1: Dynamic Programming**
74+
75+
We define $f[i][j]$ as the minimum number of operations to turn the first $i$ numbers into a beautiful array, and the $i$th number is changed to $j+1$. The answer is $\min(f[n][0], f[n][1], f[n][2])$.
76+
77+
We can enumerate all cases where the $i$th number is changed to $j+1$, and then take the minimum value. Here, we can use a rolling array to optimize the space complexity.
78+
79+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
80+
7381
<!-- tabs:start -->
7482

7583
### **Python3**

solution/2800-2899/2828.Check if a String Is an Acronym of Words/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Hence, s = &quot;ngguoy&quot; is the acronym.
5050

5151
## Solutions
5252

53+
**Solution 1: Simulation**
54+
55+
We can traverse each string in the array $words$, concatenate their first letters to form a new string $t$, and then check if $t$ is equal to $s$.
56+
57+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $words$.
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**

solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ It can be proven that there is no k-avoiding array with a sum less than 3.
3838

3939
## Solutions
4040

41+
**Solution 1: Greedy + Simulation**
42+
43+
We start from the positive integer $i=1$, and judge whether $i$ can be added to the array in turn. If it can be added, we add $i$ to the array, accumulate it to the answer, and then mark $k-i$ as visited, indicating that $k-i$ cannot be added to the array. The loop continues until the length of the array is $n$.
44+
45+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the length of the array.
46+
4147
<!-- tabs:start -->
4248

4349
### **Python3**

solution/2800-2899/2830.Maximize the Profit as the Salesman/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ It can be proven that 10 is the maximum amount of gold we can achieve.
4848

4949
## Solutions
5050

51+
**Solution 1: Sorting + Binary Search + Dynamic Programming**
52+
53+
We sort all the purchase offers by $end$ in ascending order, and then use dynamic programming to solve the problem.
54+
55+
Define $f[i]$ to represent the maximum amount of gold we can get from the first $i$ purchase offers. The answer is $f[n]$.
56+
57+
For $f[i]$, we can choose not to sell the $i$th purchase offer, in which case $f[i] = f[i - 1]$; or we can choose to sell the $i$th purchase offer, in which case $f[i] = f[j] + gold_i$, where $j$ is the largest index that satisfies $end_j \leq start_i$.
58+
59+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of purchase offers.
60+
5161
<!-- tabs:start -->
5262

5363
### **Python3**

solution/2800-2899/2831.Find the Longest Equal Subarray/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ It can be proven that no longer equal subarrays can be created.
4646

4747
## Solutions
4848

49+
**Solution 1: Hash Table + Two Pointers**
50+
51+
We use two pointers to maintain a monotonically variable length window, and a hash table to maintain the number of occurrences of each element in the window.
52+
53+
The number of all elements in the window minus the number of the most frequently occurring element in the window is the number of elements that need to be deleted from the window.
54+
55+
Each time, we add the element pointed to by the right pointer to the window, then update the hash table, and also update the number of the most frequently occurring element in the window. When the number of elements that need to be deleted from the window exceeds $k$, we move the left pointer once, and then update the hash table.
56+
57+
After the traversal, return the number of the most frequently occurring element.
58+
59+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.
60+
4961
<!-- tabs:start -->
5062

5163
### **Python3**

solution/2800-2899/2832.Maximal Range That Each Element Is Maximum in It/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ For nums[4] the longest subarray in which 6 is the maximum is nums[0..4] so ans[
4848

4949
## Solutions
5050

51+
**Solution 1: Monotonic Stack**
52+
53+
This problem is a template for monotonic stack. We only need to use the monotonic stack to find the position of the first element larger than $nums[i]$ on the left and right, denoted as $left[i]$ and $right[i]$. Then, the interval length with $nums[i]$ as the maximum value is $right[i] - left[i] - 1$.
54+
55+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**

solution/2800-2899/2833.Furthest Point From Origin/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050

5151
## Solutions
5252

53+
**Solution 1: Greedy**
54+
55+
When encountering the character '_', we can choose to move left or right. The problem requires us to find the farthest point from the origin. Therefore, we can first traverse once, greedily move all '_' to the left, and find the farthest point from the origin at this time. Then traverse again, greedily move all '\_' to the right, and find the farthest point from the origin at this time. Finally, take the maximum of the two traversals.
56+
57+
Further, we only need to calculate the difference between the number of 'L' and 'R' in the string, and then add the number of '\_'.
58+
59+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
60+
5361
<!-- tabs:start -->
5462

5563
### **Python3**

solution/2800-2899/2834.Find the Minimum Possible Sum of a Beautiful Array/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ It can be proven that 8 is the minimum possible sum that a beautiful array could
5959

6060
## Solutions
6161

62+
**Solution 1: Greedy + Hash Table**
63+
64+
We start from the positive integer $i=1$, and judge whether $i$ can be added to the array in turn. If it can be added, we add $i$ to the array, accumulate it to the answer, and then mark $target-i$ as visited, indicating that $target-i$ cannot be added to the array. The loop continues until the length of the array is $n$.
65+
66+
The time complexity is $O(n + target)$, and the space complexity is $O(n + target)$. Here, $n$ is the length of the array.
67+
6268
<!-- tabs:start -->
6369

6470
### **Python3**

solution/2800-2899/2835.Minimum Operations to Form Subsequence With Target Sum/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ It can be shown that there is no shorter sequence of operations that results in
5959

6060
## Solutions
6161

62+
**Solution 1: Greedy + Bit Manipulation**
63+
64+
Observing the operation in the problem, we find that each operation actually splits a number greater than $1$ into two equal numbers, which means that the sum of the elements in the array will not change after the operation. Therefore, if the sum of the elements in the array $s$ is less than $target$, it is impossible to obtain a subsequence with a sum of $target$ through the operation described in the problem, and we can directly return $-1$. Otherwise, we can definitely make the sum of some subsequences in the array equal to $target$ through the split operation.
65+
66+
In addition, the split operation will actually set the binary high bit of the number to $0$ and add $2$ to the lower bit. Therefore, we first use an array of length $32$ to record the number of times $1$ appears on each binary bit in the binary representation of all elements in the array $nums$.
67+
68+
Next, starting from the low bit of $target$, for the $i$th bit of $target$, if the current bit number is $0$, skip it directly, that is, $i = i + 1$. If the current bit number is $1$, we need to find the smallest number $j$ (where $j \ge i$) in the array $cnt$ such that $cnt[j] > 0$, and then we split the number $1$ at this bit to the lower bit $i$, that is, subtract $1$ from $cnt[j]$, and set each bit from $i$ to $j-1$ in $cnt$ to $1$, and the number of operations is $j-i$. Next, we let $j = i$, and then $i = i + 1$. Repeat the above operation until $i$ exceeds the index range of the array $cnt$, and return the number of operations at this time.
69+
70+
Note that if $j < i$, actually two lower bits of $1$ can be combined into a higher bit of $1$. Therefore, if $j < i$, we add $\frac{cnt[j]}{2}$ to $cnt[j+1]$, and take $cnt[j]$ modulo $2$, then let $j = j + 1$, and continue the above operation.
71+
72+
The time complexity is $O(n \times \log M)$, and the space complexity is $O(\log M)$. Here, $n$ is the length of the array $nums$, and $M$ is the maximum value in the array $nums$.
73+
6274
<!-- tabs:start -->
6375

6476
### **Python3**

solution/2800-2899/2836.Maximize Value of Function in a Ball Passing Game/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ Hence, the output is 10.
128128

129129
## Solutions
130130

131+
**Solution 1: Dynamic Programming + Binary Lifting**
132+
133+
The problem asks us to find the maximum sum of the player IDs who have touched the ball within $k$ passes starting from each player $i$. If we solve it by brute force, we need to traverse upwards $k$ times starting from $i$, with a time complexity of $O(k)$, which will obviously time out.
134+
135+
We can use dynamic programming combined with binary lifting to handle this.
136+
137+
We define $f[i][j]$ as the player ID that can be reached by passing the ball $2^j$ times starting from player $i$, and define $g[i][j]$ as the sum of the player IDs that can be reached by passing the ball $2^j$ times starting from player $i$ (excluding the last player).
138+
139+
When $j=0$, the number of passes is $1$, so $f[i][0] = receiver[i]$, and $g[i][0] = i$.
140+
141+
When $j > 0$, the number of passes is $2^j$, which is equivalent to passing the ball $2^{j-1}$ times starting from player $i$, and then passing the ball $2^{j-1}$ times starting from player $f[i][j-1]$, so $f[i][j] = f[f[i][j-1]][j-1]$, and $g[i][j] = g[i][j-1] + g[f[i][j-1]][j-1]$.
142+
143+
Next, we can enumerate each player $i$ as the starting player, then accumulate upwards according to the binary representation of $k$, and finally get the maximum sum of the player IDs who have touched the ball within $k$ passes starting from player $i$.
144+
145+
The time complexity is $O(n \times \log k)$, and the space complexity is $O(n \times \log k)$. Here, $n$ is the number of players.
146+
147+
Similar problems:
148+
149+
- [1483. Kth Ancestor of a Tree Node](/solution/1400-1499/1483.Kth%20Ancestor%20of%20a%20Tree%20Node/README_EN.md)
150+
131151
<!-- tabs:start -->
132152

133153
### **Python3**

solution/2800-2899/2837.Total Traveled Distance/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ Returning the table orderd by user_id in ascending order.</pre>
8484

8585
## Solutions
8686

87+
**Solution 1: Left Join + Group By Sum**
88+
89+
We can use a left join to connect the two tables, and then use group by sum to calculate the total distance for each user. Note that if a user has not completed any rides, their distance should be considered as $0$.
90+
8791
<!-- tabs:start -->
8892

8993
### **SQL**

solution/2800-2899/2838.Maximum Coins Heroes Can Collect/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ So the answer would be [5,16,10].</pre>
5959

6060
## Solutions
6161

62+
**Solution 1: Sorting + Prefix Sum + Binary Search**
63+
64+
We can sort the monsters and coins in ascending order of the monsters' combat power, and then use prefix sum to calculate the total number of coins each hero can get by defeating the first $i$ monsters.
65+
66+
Next, for each hero, we can use binary search to find the strongest monster he can defeat, and then use prefix sum to calculate the total number of coins he can get.
67+
68+
The time complexity is $O((m + n) \times \log n)$, and the space complexity is $O(m)$. Here, $m$ and $n$ are the number of monsters and heroes, respectively.
69+
6270
<!-- tabs:start -->
6371

6472
### **Python3**

solution/2800-2899/2839.Check if Strings Can be Made Equal With Operations I/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Counting**
47+
48+
We observe the operation in the problem, and find that if the parity of the two indices $i$ and $j$ of the string is the same, then their order can be changed by swapping.
49+
50+
Therefore, we can count the occurrence times of the characters at odd indices and even indices in the two strings. If the counting results of the two strings are the same, then we can make the two strings equal through the operation.
51+
52+
The time complexity is $O(n + |\Sigma|)$, and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string, and $\Sigma$ is the character set.
53+
54+
Similar problems:
55+
56+
- [2840. Check if Strings Can be Made Equal With Operations II](/solution/2800-2899/2840.Check%20if%20Strings%20Can%20be%20Made%20Equal%20With%20Operations%20II/README_EN.md)
57+
4658
<!-- tabs:start -->
4759

4860
### **Python3**

solution/2800-2899/2840.Check if Strings Can be Made Equal With Operations II/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@
4545

4646
## Solutions
4747

48+
**Solution 1: Counting**
49+
50+
We observe the operation in the problem, and find that if the parity of the two indices $i$ and $j$ of the string is the same, then their order can be changed by swapping.
51+
52+
Therefore, we can count the occurrence times of the characters at odd indices and even indices in the two strings. If the counting results of the two strings are the same, then we can make the two strings equal through the operation.
53+
54+
The time complexity is $O(n + |\Sigma|)$, and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string, and $\Sigma$ is the character set.
55+
56+
Similar problems:
57+
58+
- [2839. Check if Strings Can be Made Equal With Operations I](/solution/2800-2899/2839.Check%20if%20Strings%20Can%20be%20Made%20Equal%20With%20Operations%20I/README_EN.md)
59+
4860
<!-- tabs:start -->
4961

5062
### **Python3**

solution/2800-2899/2841.Maximum Sum of Almost Unique Subarray/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848

4949
## Solutions
5050

51+
**Solution 1: Sliding Window + Hash Table**
52+
53+
We can traverse the array $nums$, maintain a window of size $k$, use a hash table $cnt$ to count the occurrence of each element in the window, and use a variable $s$ to sum all elements in the window. If the number of different elements in $cnt$ is greater than or equal to $m$, then we update the answer $ans = \max(ans, s)$.
54+
55+
After the traversal ends, return the answer.
56+
57+
The time complexity is $O(n)$, and the space complexity is $O(k)$. Here, $n$ is the length of the array.
58+
5159
<!-- tabs:start -->
5260

5361
### **Python3**

solution/2800-2899/2842.Count K-Subsequences of a String With Maximum Beauty/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ Hence, the answer is 2.
7777

7878
## Solutions
7979

80+
**Solution 1: Greedy + Combinatorial Mathematics**
81+
82+
First, we use a hash table $f$ to count the occurrence of each character in the string $s$, i.e., $f[c]$ represents the number of times character $c$ appears in the string $s$.
83+
84+
Since a $k$-subsequence is a subsequence of length $k$ in the string $s$ with unique characters, if the number of different characters in $f$ is less than $k$, then there is no $k$-subsequence, and we can directly return $0$.
85+
86+
Otherwise, to maximize the beauty value of the $k$-subsequence, we need to make characters with high beauty values appear as much as possible in the $k$-subsequence. Therefore, we can sort the values in $f$ in reverse order to get an array $vs$.
87+
88+
We denote the occurrence of the $k$th character in the array $vs$ as $val$, and there are $x$ characters with an occurrence of $val$.
89+
90+
Then we first find out the characters with occurrences greater than $val$, multiply the occurrences of each character to get the initial answer $ans$, and update the remaining number of characters to be selected to $k$. We need to select $k$ characters from $x$ characters, so the answer needs to be multiplied by the combination number $C_x^k$, and finally multiplied by $val^k$, i.e., $ans = ans \times C_x^k \times val^k$.
91+
92+
Note that we need to use fast power and modulo operations here.
93+
94+
The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string, and $\Sigma$ is the character set. In this problem, the character set is lowercase letters, so $|\Sigma| = 26$.
95+
8096
<!-- tabs:start -->
8197

8298
### **Python3**

solution/2800-2899/2843.Count Symmetric Integers/README_EN.md

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

3737
## Solutions
3838

39+
**Solution 1: Enumeration**
40+
41+
We enumerate each integer $x$ in the range $[low, high]$, and check whether it is a palindromic number. If it is, then the answer $ans$ is increased by $1$.
42+
43+
The time complexity is $O(n \times \log m)$, and the space complexity is $O(\log m)$. Here, $n$ is the number of integers in the range $[low, high]$, and $m$ is the maximum integer given in the problem.
44+
3945
<!-- tabs:start -->
4046

4147
### **Python3**

solution/2800-2899/2844.Minimum Operations to Make a Special Number/README_EN.md

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ It can be shown that 1 is the minimum number of operations required to get a spe
5050

5151
## Solutions
5252

53+
**Solution 1: Memoization Search**
54+
55+
We notice that an integer $x$ can be divisible by $25$, i.e., $x \bmod 25 = 0$. Therefore, we can design a function $dfs(i, k)$, which represents the minimum number of digits to be deleted to make the number a special number, starting from the $i$th digit of the string $num$, and the current number modulo $25$ is $k$. The answer is $dfs(0, 0)$.
56+
57+
The execution logic of the function $dfs(i, k)$ is as follows:
58+
59+
- If $i = n$, i.e., all digits of the string $num$ have been processed, then if $k = 0$, the current number can be divisible by $25$, return $0$, otherwise return $n$;
60+
- Otherwise, the $i$th digit can be deleted, in this case one digit needs to be deleted, i.e., $dfs(i + 1, k) + 1$; if the $i$th digit is not deleted, then the value of $k$ becomes $(k \times 10 + \textit{num}[i]) \bmod 25$, i.e., $dfs(i + 1, (k \times 10 + \textit{num}[i]) \bmod 25)$. Take the minimum of these two.
61+
62+
To prevent repeated calculations, we can use memoization to optimize the time complexity.
63+
64+
The time complexity is $O(n \times 25)$, and the space complexity is $O(n \times 25)$. Here, $n$ is the length of the string $num$.
65+
5366
<!-- tabs:start -->
5467

5568
### **Python3**

solution/2800-2899/2845.Count of Interesting Subarrays/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ It can be shown that there are no other interesting subarrays. So, the answer is
6262

6363
## Solutions
6464

65+
**Solution 1: Hash Table + Prefix Sum**
66+
67+
The problem requires the number of indices $i$ in an interval that satisfy $nums[i] \bmod modulo = k$. We can transform the array $nums$ into a $0-1$ array $arr$, where $arr[i] = 1$ indicates $nums[i] \bmod modulo = k$, otherwise $arr[i] = 0$.
68+
69+
For an interval $[l, r]$, we can calculate the number of $1$s in $arr[l..r]$ through the prefix sum array $s$, i.e., $s[r] - s[l - 1]$, where $s[0] = 0$.
70+
71+
We use a hash table $cnt$ to record the number of occurrences of the prefix sum $s \bmod modulo$, initially $cnt[0]=1$.
72+
73+
Next, we traverse the array $arr$, calculate the prefix sum $s$, add the number of occurrences of $(s-k) \bmod modulo$ to the answer, and then add $1$ to the number of occurrences of $s \bmod modulo$.
74+
75+
After the traversal ends, return the answer.
76+
77+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
78+
6579
<!-- tabs:start -->
6680

6781
### **Python3**

0 commit comments

Comments
 (0)