Skip to content

Commit f3de1c7

Browse files
committed
feat: add solutions to lc problems
1 parent 56ddfc6 commit f3de1c7

File tree

10 files changed

+120
-2
lines changed

10 files changed

+120
-2
lines changed

solution/1000-1099/1023.Camelcase Matching/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@
5757

5858
我们可以遍历 `queries` 中的每个字符串,判断其是否与 `pattern` 匹配,若匹配则将 `true` 加入答案数组,否则加入 `false`
5959

60-
判断两个字符串是否匹配,我们可以使用双指针 $i$ 和 $j$,分别指向两个字符串的首字符,然后遍历两个字符串,如果指针 $i$ 指向的字符与指针 $j$ 指向的字符不同,则判断指针 $i$ 指向的字符是否为小写字母,若是,则指针 $i$ 循环向后移动。如果指针 $i$ 移动到字符串末尾,或者指针 $i$ 指向的字符与指针 $j$ 指向的字符不同,说明两个字符串不匹配,返回 `false`。否则,指针 $i$ 和 $j$ 同时向后移动一位,继续判断
60+
接下来,我们实现一个 $check(s, t)$ 函数,用于判断字符串 $s$ 和 $t$ 是否匹配
6161

62-
时间复杂度 $O(\sum_{i=0}^{n-1}q_i + n \times m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别为 `queries``pattern` 的长度,而 $q_i$ 为 `queries[i]` 的长度。
62+
我们可以使用双指针 $i$ 和 $j$,分别指向两个字符串的首字符,然后遍历两个字符串。如果指针 $i$ 和 $j$ 指向的字符不同,并且 $s[i]$ 为小写字母,则指针 $i$ 循环向后移动一位。
63+
64+
如果指针 $i$ 已经到达字符串 $s$ 的末尾,或者指针 $i$ 和 $j$ 指向的字符不同,则返回 `false`。否则,指针 $i$ 和 $j$ 同时向后移动一位。当指针 $j$ 到达字符串 $t$ 的末尾时,我们需要判断字符串 $s$ 中剩余的字符是否都为小写字母,若是则返回 `true`,否则返回 `false`
65+
66+
时间复杂度 $(n \times m)$,其中 $n$ 和 $m$ 分别为数组 `queries` 的长度和字符串 `pattern` 的长度。
6367

6468
<!-- tabs:start -->
6569

solution/1000-1099/1023.Camelcase Matching/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@
4747

4848
## Solutions
4949

50+
**Approach 1: Two Pointers**
51+
52+
We can traverse every string in `queries` and check whether it matches `pattern` or not. If it matches, we add `true` to the answer array, otherwise we add `false`.
53+
54+
Next, we implement a function $check(s, t)$ to check whether the string $s$ matches the string $t$.
55+
56+
We can use two pointers $i$ and $j$ to traverse the two strings. If the characters pointed to by $i$ and $j$ are not the same and $s[i]$ is a lowercase letter, then we move the pointer $i$ to the next position.
57+
58+
If the pointer $i$ has reached the end of the string $s$ or the characters pointed to by $i$ and $j$ are not the same, we return `false`. Otherwise, we move both pointers $i$ and $j$ to the next position. When the pointer $j$ reaches the end of the string $t$, we need to check if the remaining characters in the string $s$ are all lowercase letters. If so, we return `true`, otherwise we return `false`.
59+
60+
Time complexity $(n \times m)$, where $n$ and $m$ are the length of the array `queries` and the string `pattern` respectively.
61+
5062
<!-- tabs:start -->
5163

5264
### **Python3**

solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ It can be shown that we cannot have less than 3 rows in a valid array.</pre>
4747

4848
## Solutions
4949

50+
**Approach 1: Array or Hash Table**
51+
52+
We use an array or hash table $cnt$ to count the number of occurrences of each element in the array $nums$.
53+
54+
Then we traverse the $cnt$ array, add $x$ to the $0$th row, the $1$st row, the $2$nd row, ..., the ($cnt[x]-1$)th row of the answer list.
55+
56+
Finally, return the answer list.
57+
58+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
59+
5060
<!-- tabs:start -->
5161

5262
### **Python3**

solution/2600-2699/2611.Mice and Cheese/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ It can be proven that 2 is the maximum total points that the mice can achieve.
4949

5050
## Solutions
5151

52+
**Approach 1: Greedy + Sort**
53+
54+
We can first give all the cheese to the second mouse. Next, consider giving $k$ pieces of cheese to the first mouse. How should we choose these $k$ pieces of cheese? Obviously, if we give the $i$-th piece of cheese from the second mouse to the first mouse, the change in the score is $reward1[i] - reward2[i]$. We hope that this change is as large as possible, so that the total score is maximized.
55+
56+
Therefore, we sort the cheese in decreasing order of `reward1[i] - reward2[i]`. The first $k$ pieces of cheese are eaten by the first mouse, and the remaining cheese is eaten by the second mouse to obtain the maximum score.
57+
58+
Time complexity $O(n \times \log n)$, space complexity $O(n)$. Where $n$ is the number of cheeses.
59+
5260
<!-- tabs:start -->
5361

5462
### **Python3**

solution/2600-2699/2612.Minimum Reverse Operations/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@
5858

5959
## Solutions
6060

61+
**Approach 1: Ordered Set + BFS**
62+
63+
We notice that for any index $i$ in the subarray interval $[l,..r]$, the flipped index $j = l + r - i$.
64+
65+
If the subarray moves one position to the right, then $j = l + 1 + r + 1 - i = l + r - i + 2$, that is, $j$ will increase by $2$.
66+
67+
Similarly, if the subarray moves one position to the left, then $j = l - 1 + r - 1 - i = l + r - i - 2$, that is, $j$ will decrease by $2$.
68+
69+
Therefore, for a specific index $i$, all its flipped indices form an arithmetic progression with common difference $2$, that is, all the flipped indices have the same parity.
70+
71+
Next, we consider the range of values ​​of the index $i$ after flipping $j$.
72+
73+
- If the boundary is not considered, the range of values ​​of $j$ is $[i - k + 1, i + k - 1]$.
74+
- If the subarray is on the left, then $[l, r] = [0, k - 1]$, so the flipped index $j$ of $i$ is $0 + k - 1 - i$, that is, $j = k - i - 1$, so the left boundary $mi = max(i - k + 1, k - i - 1)$.
75+
- If the subarray is on the right, then $[l, r] = [n - k, n - 1]$, so the flipped index $j= n - k + n - 1 - i$ is $j = n \times 2 - k - i - 1$, so the right boundary of $j$ is $mx = min(i + k - 1, n \times 2 - k - i - 1)$.
76+
77+
We use two ordered sets to store all the odd indices and even indices to be searched, here we need to exclude the indices in the array $banned$ and the index $p$.
78+
79+
Then we use BFS to search, each time searching all the flipped indices $j$ of the current index $i$, that is, $j = mi, mi + 2, mi + 4, \dots, mx$, updating the answer of index $j$ and adding index $j$ to the search queue, and removing index $j$ from the corresponding ordered set.
80+
81+
When the search is over, the answer to all indices can be obtained.
82+
83+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$. Where $n$ is the given array length in the problem.
84+
6185
<!-- tabs:start -->
6286

6387
### **Python3**

solution/2600-2699/2613.Beautiful Pairs/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@
4444

4545
## Solutions
4646

47+
**Approach 1: Sorting + Divide and Conquer**
48+
49+
This problem is equivalent to finding two points in the plane, such that the Manhattan distance between them is the smallest. If there are multiple points satisfying the condition, return the one with the smallest index.
50+
51+
First, we handle the case where there are duplicate points. For each point, we record the corresponding indices in a list. If the length of the index list is greater than $1$, then the first two indices in the index list can be used as candidates, and we find the smallest index pair.
52+
53+
If there are no duplicate points, we sort all the points by $x$ coordinates, and then use the divide and conquer to solve the problem.
54+
55+
For each interval $[l, r]$, we first calculate the median of the $x$ coordinates $m$, and then recursively solve the left and right intervals, and get $d_1, (pi_1, pj_1)$ and $d_2, (pi_2, pj_2)$ respectively, where $d_1$ and $d_2$ are the minimum Manhattan distances of the left and right intervals respectively, and $(pi_1, pj_1)$ and $(pi_2, pj_2)$ are the index pairs of the two points of the minimum Manhattan distance of the left and right intervals respectively. We take the smaller one of $d_1$ and $d_2$ as the minimum Manhattan distance of the current interval, and if $d_1 = d_2$, we take the one with the smaller index as the answer. The corresponding two points of the index are taken as the answer.
56+
57+
The above considers the case where the two points are on the same side. If the two points are on different sides, we take the middle point, i.e. the point with the index of $m = \lfloor (l + r) / 2 \rfloor$ as the standard, and divide a new region. The range of this region is to expand the range of $d_1$ from the middle point to the left and right sides respectively. Then we sort these points in the range by $y$ coordinates, and then traverse each point pair in the sorted order. If the difference of the $y$ coordinates of the two points is greater than the current minimum Manhattan distance, then the following point pairs do not need to be considered, because their $y$ coordinate differences are larger, so the Manhattan distance is larger, and it will not be smaller than the current minimum Manhattan distance. Otherwise, we update the minimum Manhattan distance, and update the answer.
58+
59+
Finally, we return the answer.
60+
61+
Time complexity: $O(n \times \log n)$, where $n$ is the length of the array.
62+
63+
Space complexity: $O(n)$.
64+
4765
<!-- tabs:start -->
4866

4967
### **Python3**

solution/2600-2699/2614.Prime In Diagonal/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747

4848
## Solutions
4949

50+
**Approach 1: Math + Simulation**
51+
52+
We implement a function `is_prime` to check whether a number is prime.
53+
54+
Then we iterate the array and check whether the numbers on the diagonals are prime. If so, we update the answer.
55+
56+
The time complexity is $O(n \times \sqrt{M})$, where $n$ and $M$ are the number of rows of the array and the maximum value in the array, respectively. The space complexity is $O(1)$.
57+
5058
<!-- tabs:start -->
5159

5260
### **Python3**

solution/2600-2699/2616.Minimize the Maximum Difference of Pairs/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0,
3939

4040
## Solutions
4141

42+
**Approach 1: Binary search + Greedy**
43+
44+
We find that the maximum difference has the monotonicity, that is, if the maximum difference $x$ satisfies the condition, then $x-1$ must also satisfy the condition. Therefore, we can use the binary search method to find the smallest maximum difference that satisfies the condition.
45+
46+
We can sort the array `nums`, then enumerate the maximum difference $x$, and determine whether there are $p$ index pairs, where each index pair corresponds to the maximum value of the difference of the corresponding value. If it exists, we can reduce $x$, otherwise we can increase $x$.
47+
48+
Determine whether there are $p$ index pairs, where each index pair corresponds to the maximum value of the difference of the corresponding value, which can be achieved by using the greedy method. We traverse the array `nums` from left to right, and for the current traversed index $i$, if the difference between the number at the $i+1$ position and the number at the $i$ position is no more than $x$, then we can take the number at the $i$ and $i+1$ positions as an index pair, update the number of index pairs $cnt$, and then increase the value of $i$ by $2$. Otherwise, we will increase the value of $i$ by $1$. When the traversal is over, if the value of $cnt$ is greater than or equal to $p$, then it means that there are $p$ index pairs, where each index pair corresponds to the maximum value of the difference of the corresponding value, otherwise it means that it does not exist.
49+
50+
The time complexity is $O(n \times (\log n + \log m))$, where $n$ is the length of the array `nums`, and $m$ is the difference between the maximum value and the minimum value in the array `nums`. The space complexity is $O(1)$.
51+
4252
<!-- tabs:start -->
4353

4454
### **Python3**

solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@
5454

5555
## Solutions
5656

57+
**Approach 1: Priority Queue**
58+
59+
Let's denote the number of rows of the grid as $m$ and the number of columns as $n$. Define $dist[i][j]$ to be the shortest distance from the coordinate $(0, 0)$ to the coordinate $(i, j)$. Initially, $dist[0][0]=1$ and $dist[i][j]=-1$ for all other $i$ and $j$.
60+
61+
For each grid $(i, j)$, it can come from the grid above or the grid on the left. If it comes from the grid above $(i', j)$, where $0 \leq i' \lt i$, then $(i', j)$ must satisfy $grid[i'][j] + i' \geq i$. We need to select from these grids the one that is closest.
62+
63+
Therefore, we maintain a priority queue (min-heap) for each column $j$. Each element of the priority queue is a pair $(dist[i][j], i)$, which represents that the shortest distance from the coordinate $(0, 0)$ to the coordinate $(i, j)$ is $dist[i][j]$. When we consider the coordinate $(i, j)$, we only need to take out the head element $(dist[i'][j], i')$ of the priority queue. If $grid[i'][j] + i' \geq i$, we can move from the coordinate $(i', j)$ to the coordinate $(i, j)$. At this time, we can update the value of $dist[i][j]$, that is, $dist[i][j] = dist[i'][j] + 1$, and add $(dist[i][j], i)$ to the priority queue.
64+
65+
Similarly, we can maintain a priority queue for each row $i$ and perform a similar operation.
66+
67+
Finally, we can obtain the shortest distance from the coordinate $(0, 0)$ to the coordinate $(m - 1, n - 1)$, that is, $dist[m - 1][n - 1]$, which is the answer.
68+
69+
The time complexity is $O(m \times n \times \log (m \times n))$ and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns of the grid, respectively.
70+
5771
<!-- tabs:start -->
5872

5973
### **Python3**

solution/2600-2699/2638.Count the Number of K-Free Subsets/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@
4848

4949
## Solutions
5050

51+
**Approach 1: Grouping + Dynamic Programming**
52+
53+
First, sort the array $nums$ in ascending order, and then group the elements in the array according to the remainder modulo $k$, that is, the elements $nums[i] \bmod k$ with the same remainder are in the same group. Then for any two elements in different groups, their absolute difference is not equal to $k$. Therefore, we can obtain the number of subsets in each group, and then multiply the number of subsets in each group to obtain the answer.
54+
55+
For each group $arr$, we can use dynamic programming to obtain the number of subsets. Let $f[i]$ denote the number of subsets of the first $i$ elements, and initially $f[0] = 1$, and $f[1]=2$. When $i \geq 2$, if $arr[i-1]-arr[i-2]=k$, if we choose $arr[i-1]$, then $f[i]=f[i-2]$; If we do not choose $arr[i-1]$, then $f[i]=f[i-1]$. Therefore, when $arr[i-1]-arr[i-2]=k$, we have $f[i]=f[i-1]+f[i-2]$; otherwise $f[i] = f[i - 1] \times 2$. The number of subsets of this group is $f[m]$, where $m$ is the length of the array $arr$.
56+
57+
Finally, we multiply the number of subsets of each group to obtain the answer.
58+
59+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
60+
5161
<!-- tabs:start -->
5262

5363
### **Python3**

0 commit comments

Comments
 (0)