Skip to content

Commit 6a6cc3f

Browse files
authored
feat: add solutions to lc problems (doocs#2089)
1 parent f901522 commit 6a6cc3f

File tree

125 files changed

+1150
-85
lines changed

Some content is hidden

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

125 files changed

+1150
-85
lines changed

solution/2300-2399/2300.Successful Pairs of Spells and Potions/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ Thus, [2,0,2] is returned.
4848

4949
## Solutions
5050

51+
**Solution 1: Sorting + Binary Search**
52+
53+
We can sort the potion array, then traverse the spell array. For each spell $v$, we use binary search to find the first potion that is greater than or equal to $\frac{success}{v}$. We mark its index as $i$. The length of the potion array minus $i$ is the number of potions that can successfully combine with this spell.
54+
55+
The time complexity is $O((m + n) \times \log m)$, and the space complexity is $O(\log n)$. Here, $m$ and $n$ are the lengths of the potion array and the spell array, respectively.
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**

solution/2300-2399/2301.Match Substring After Replacement/README_EN.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ Now sub = &quot;l33tb&quot; is a substring of s, so we return true.
5858

5959
## Solutions
6060

61+
**Solution 1: Hash Table + Enumeration**
62+
63+
First, we use a hash table $d$ to record the set of characters that each character can be replaced with.
64+
65+
Then we enumerate all substrings of length $sub$ in $s$, and judge whether the string $sub$ can be obtained by replacement. If it can, return `true`, otherwise enumerate the next substring.
66+
67+
At the end of the enumeration, it means that $sub$ cannot be obtained by replacing any substring in $s$, so return `false`.
68+
69+
The time complexity is $O(m \times n)$, and the space complexity is $O(C^2)$. Here, $m$ and $n$ are the lengths of the strings $s$ and $sub$ respectively, and $C$ is the size of the character set.
70+
71+
**Solution 2: Array + Enumeration**
72+
73+
Since the character set only contains uppercase and lowercase English letters and numbers, we can directly use a $128 \times 128$ array $d$ to record the set of characters that each character can be replaced with.
74+
75+
The time complexity is $O(m \times n)$, and the space complexity is $O(C^2)$.
76+
6177
<!-- tabs:start -->
6278

6379
### **Python3**

solution/2300-2399/2302.Count Subarrays With Score Less Than K/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@
6060

6161
**方法一:前缀和 + 二分查找**
6262

63-
我们先计算出数组 `nums` 的前缀和数组 $s$,其中 $s[i]$ 表示数组 `nums` 前 $i$ 个元素的和。
63+
我们先计算出数组 $nums$ 的前缀和数组 $s$,其中 $s[i]$ 表示数组 $nums$ 前 $i$ 个元素的和。
6464

65-
接下来,我们枚举数组 `nums` 每个元素作为子数组的最后一个元素,对于每个元素,我们可以通过二分查找的方式找到最大的长度 $l$,使得 $s[i] - s[i - l] \times l < k$。那么以该元素为最后一个元素的子数组个数即为 $l$,我们将所有的 $l$ 相加即为答案。
65+
接下来,我们枚举数组 $nums$ 每个元素作为子数组的最后一个元素,对于每个元素,我们可以通过二分查找的方式找到最大的长度 $l$,使得 $s[i] - s[i - l] \times l < k$。那么以该元素为最后一个元素的子数组个数即为 $l$,我们将所有的 $l$ 相加即为答案。
6666

67-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
67+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
6868

6969
**方法二:双指针**
7070

7171
我们可以使用双指针的方式,维护一个滑动窗口,使得窗口内的元素和小于 $k$。那么以当前元素为最后一个元素的子数组个数即为窗口的长度,我们将所有的窗口长度相加即为答案。
7272

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

7575
<!-- tabs:start -->
7676

solution/2300-2399/2302.Count Subarrays With Score Less Than K/README_EN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ Thus, there are 5 subarrays having scores less than 5.
5252

5353
## Solutions
5454

55+
**Solution 1: Prefix Sum + Binary Search**
56+
57+
First, we calculate the prefix sum array $s$ of the array $nums$, where $s[i]$ represents the sum of the first $i$ elements of the array $nums$.
58+
59+
Next, we enumerate each element of the array $nums$ as the last element of the subarray. For each element, we can find the maximum length $l$ such that $s[i] - s[i - l] \times l < k$ by binary search. The number of subarrays with this element as the last element is $l$, and we add all $l$ to get the answer.
60+
61+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
62+
63+
**Solution 2: Two Pointers**
64+
65+
We can use two pointers to maintain a sliding window, so that the sum of the elements in the window is less than $k$. The number of subarrays with the current element as the last element is the length of the window, and we add all window lengths to get the answer.
66+
67+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
68+
5569
<!-- tabs:start -->
5670

5771
### **Python3**

solution/2300-2399/2303.Calculate Amount Paid in Taxes/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@
7070

7171
**方法一:模拟**
7272

73-
遍历 `brackets`,对于每个税级,计算该税级的税额,然后累加即可。
73+
我们遍历 `brackets`,对于每个税级,计算该税级的税额,然后累加即可。
7474

75-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 `brackets` 的长度。
75+
时间复杂度 $O(n)$,其中 $n$ 为 `brackets` 的长度。空间复杂度 $O(1)$
7676

7777
<!-- tabs:start -->
7878

solution/2300-2399/2303.Calculate Amount Paid in Taxes/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ You have no income to tax, so you have to pay a total of 0 in taxes.
6464

6565
## Solutions
6666

67+
**Solution 1: Simulation**
68+
69+
We traverse `brackets`, and for each tax bracket, we calculate the tax amount for that bracket, then accumulate it.
70+
71+
The time complexity is $O(n)$, where $n$ is the length of `brackets`. The space complexity is $O(1)$.
72+
6773
<!-- tabs:start -->
6874

6975
### **Python3**

solution/2300-2399/2305.Fair Distribution of Cookies/README_EN.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ It can be shown that there is no distribution with an unfairness less than 7.
4747

4848
## Solutions
4949

50+
**Solution 1: Backtracking + Pruning**
51+
52+
First, we sort the array $cookies$ in descending order (to reduce the number of searches), and then create an array $cnt$ of length $k$ to store the number of cookies each child gets. Also, we use a variable $ans$ to maintain the current minimum degree of unfairness, initialized to a very large value.
53+
54+
Next, we start from the first snack pack. For the current snack pack $i$, we enumerate each child $j$. If the cookies $cookies[i]$ in the current snack pack are given to child $j$, making the degree of unfairness greater than or equal to $ans$, or the number of cookies the current child already has is the same as the previous child, then we don't need to consider giving the cookies in the current snack pack to child $j$, just skip it (pruning). Otherwise, we give the cookies $cookies[i]$ in the current snack pack to child $j$, and then continue to consider the next snack pack. When we have considered all the snack packs, we update the value of $ans$, then backtrack to the previous snack pack, and continue to enumerate which child to give the cookies in the current snack pack to.
55+
56+
Finally, we return $ans$.
57+
5058
<!-- tabs:start -->
5159

5260
### **Python3**

solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
接下来我们只要获取 $mask$ 的二进制表示中最高位的 $1$ 的位置,将其转换为对应的大写字母即可。如果所有二进制位都不为 $1$,说明不存在大小写同时出现的字母,返回空字符串。
7272

73-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是字符串 $s$ 的长度。
73+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$
7474

7575
<!-- tabs:start -->
7676

solution/2300-2399/2309.Greatest English Letter in Upper and Lower Case/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ There is no letter that appears in both lower and upper case.
4747

4848
## Solutions
4949

50+
**Solution 1: Hash Table + Enumeration**
51+
52+
First, we use a hash table $ss$ to record all the letters that appear in the string $s$. Then we start enumerating from the last letter of the uppercase alphabet. If both the uppercase and lowercase forms of the current letter are in $ss$, we return that letter.
53+
54+
At the end of the enumeration, if no letter that meets the conditions is found, we return an empty string.
55+
56+
The time complexity is $O(n)$, and the space complexity is $O(C)$. Here, $n$ and $C$ are the length of the string $s$ and the size of the character set, respectively.
57+
58+
**Solution 2: Bit Manipulation (Space Optimization)**
59+
60+
We can use two integers $mask1$ and $mask2$ to record the lowercase and uppercase letters that appear in the string $s$, respectively. The $i$-th bit of $mask1$ indicates whether the $i$-th lowercase letter appears, and the $i$-th bit of $mask2$ indicates whether the $i$-th uppercase letter appears.
61+
62+
Then we perform a bitwise AND operation on $mask1$ and $mask2$. The $i$-th bit of the resulting $mask$ indicates whether the $i$-th letter appears in both uppercase and lowercase.
63+
64+
Next, we just need to get the position of the highest $1$ in the binary representation of $mask$, and convert it to the corresponding uppercase letter. If all binary bits are not $1$, it means that there is no letter that appears in both uppercase and lowercase, so we return an empty string.
65+
66+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
67+
5068
<!-- tabs:start -->
5169

5270
### **Python3**

solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README_EN.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ It can be proven that no other way is possible, so we return 3.</pre>
4141

4242
## Solutions
4343

44+
**Solution 1: Memorization Search**
45+
46+
We design a function $dfs(i, j)$, which represents the number of ways to reach the target position when the current position is $i$ distance from the target position and there are $j$ steps left. The answer is $dfs(abs(startPos - endPos), k)$.
47+
48+
The calculation method of the function $dfs(i, j)$ is as follows:
49+
50+
- If $i \gt j$ or $j \lt 0$, it means that the current distance from the target position is greater than the remaining steps, or the remaining steps are negative. In this case, it is impossible to reach the target position, so return $0$;
51+
- If $j = 0$, it means that there are no steps left. At this time, only when the current distance from the target position is $0$ can the target position be reached, otherwise it is impossible to reach the target position. Return $1$ or $0$;
52+
- Otherwise, the current distance from the target position is $i$, and there are $j$ steps left. There are two ways to reach the target position:
53+
- Move one step to the left, the current distance from the target position is $i + 1$, and there are $j - 1$ steps left. The number of methods is $dfs(i + 1, j - 1)$;
54+
- Move one step to the right, the current distance from the target position is $abs(i - 1)$, and there are $j - 1$ steps left. The number of methods is $dfs(abs(i - 1), j - 1)$;
55+
- Finally, return the result of the sum of the two methods modulo $10^9 + 7$.
56+
57+
To avoid repeated calculations, we use memorization search, that is, we use a two-dimensional array $f$ to record the result of the function $dfs(i, j)$. When the function $dfs(i, j)$ is called, if $f[i][j]$ is not $-1$, return $f[i][j]$ directly, otherwise calculate the value of $f[i][j]$, and return $f[i][j]$.
58+
59+
The time complexity is $O(k^2)$, and the space complexity is $O(k^2)$. Here, $k$ is the number of steps given in the problem.
60+
4461
<!-- tabs:start -->
4562

4663
### **Python3**

0 commit comments

Comments
 (0)