Skip to content

Commit 9480261

Browse files
authored
feat: add solutions to lc problems: No.2824~2867 (doocs#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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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**

0 commit comments

Comments
 (0)