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

+6
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

+16
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

+4-4
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

+14
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

+2-2
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

+6
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

+8
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

+1-1
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

+18
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

+17
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**

solution/2400-2499/2401.Longest Nice Subarray/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ It can be proven that no longer nice subarray can be obtained, so we return 3.</
4444

4545
## Solutions
4646

47+
**Solution 1: Two Pointers**
48+
49+
We define a variable $mask$ to record the bitwise OR result of the elements in the current subarray, initially $mask = 0$. Also, we use two pointers $j$ and $i$ to point to the left and right endpoints of the current subarray, initially $i = j = 0$.
50+
51+
Next, we traverse the array $nums$ from left to right. For each element $x$ we encounter:
52+
53+
We perform a bitwise AND operation between it and $mask$. If the result is not $0$, it means that $x$ and at least one element in $mask$ have a binary representation where a certain bit is $1$, and the corresponding bit in the other element's binary representation is $0$. Such pairs of elements cannot satisfy the problem's requirements, so we need to move $j$ to the right until the bitwise AND result of $x$ and $mask$ is $0$.
54+
55+
At this point, we have found a subarray that satisfies the problem's requirements. Its length is $i - j + 1$. We compare it with the length of the current longest elegant subarray. If it is longer than the current longest elegant subarray, we update the length of the longest elegant subarray.
56+
57+
Then we perform a bitwise OR operation between $mask$ and $x$, and continue to the next element.
58+
59+
Finally, the length of the longest elegant subarray we obtain is the answer.
60+
61+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $nums$.
62+
4763
<!-- tabs:start -->
4864

4965
### **Python3**

solution/2400-2499/2402.Meeting Rooms III/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,19 @@
7171

7272
**方法一:优先队列(小根堆)**
7373

74-
定义两个优先队列,分别表示空闲会议室、使用中的会议室。其中:空闲会议室 `idle` 依据**下标**排序;而使用中的会议室 `busy` 依据**结束时间、下标**排序。
74+
我们定义两个优先队列,分别表示空闲会议室、使用中的会议室。其中:空闲会议室 `idle` 依据**下标**排序;而使用中的会议室 `busy` 依据**结束时间、下标**排序。
7575

7676
先对会议按照开始时间排序,然后遍历会议,对于每个会议:
7777

7878
- 若有使用中的会议室小于当前等于会议的开始时间,将其加入到空闲会议室队列 `idle` 中;
7979
- 若当前有空闲会议室,那么在空闲队列 `idle` 中取出权重最小的会议室,将其加入使用中的队列 `busy` 中;
8080
- 若当前没有空闲会议室,那么在使用队列 `busy` 中找出最早结束时间且下标最小的会议室,重新加入使用中的队列 `busy` 中。
8181

82-
时间复杂度 $O(m\log m)$,其中 $m$ 为会议数量。
82+
时间复杂度 $O(m \times \log m)$,其中 $m$ 为会议数量。
8383

84-
相似题目:[1882. 使用服务器处理任务](/solution/1800-1899/1882.Process%20Tasks%20Using%20Servers/README.md)
84+
相似题目:
85+
86+
- [1882. 使用服务器处理任务](/solution/1800-1899/1882.Process%20Tasks%20Using%20Servers/README.md)
8587

8688
<!-- tabs:start -->
8789

solution/2400-2499/2402.Meeting Rooms III/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
6565

6666
## Solutions
6767

68+
**Solution 1: Priority Queue (Min Heap)**
69+
70+
We define two priority queues, representing idle meeting rooms and busy meeting rooms, respectively. Among them: the idle meeting rooms `idle` are sorted according to **index**; while the busy meeting rooms `busy` are sorted according to **end time, index**.
71+
72+
First, sort the meetings by start time, then traverse the meetings. For each meeting:
73+
74+
- If there is a busy meeting room that is less than or equal to the start time of the current meeting, add it to the idle meeting room queue `idle`.
75+
- If there are currently idle meeting rooms, take out the meeting room with the smallest weight from the idle queue `idle` and add it to the busy queue `busy`.
76+
- If there are currently no idle meeting rooms, find the meeting room with the earliest end time and smallest index in the busy queue `busy`, and re-add it to the busy queue `busy`.
77+
78+
The time complexity is $O(m \times \log m)$, where $m$ is the number of meetings.
79+
80+
Similar problems:
81+
82+
- [1882. Process Tasks Using Servers](/solution/1800-1899/1882.Process%20Tasks%20Using%20Servers/README_EN.md)
83+
6884
<!-- tabs:start -->
6985

7086
### **Python3**

solution/2400-2499/2403.Minimum Time to Kill All Monsters/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@
7979

8080
<!-- 这里可写通用的实现逻辑 -->
8181

82-
**方法一:状态压缩 + 记忆化搜索/动态规划**
82+
**方法一:状态压缩 + 记忆化搜索或动态规划**
8383

84-
由于打怪才能增加每天法力的收益 `gain`,不同的打怪顺序对结果有影响,需要枚举。注意到题目的数据范围较小,考虑使用状态压缩动态规划求解。
84+
由于打怪才能增加每天法力的收益 $gain$,不同的打怪顺序对结果有影响,需要枚举。注意到题目的数据范围较小,考虑使用状态压缩动态规划求解。
8585

86-
我们定义状态 `mask` 表示当前已经打怪的情况,其二进制中的 `1` 表示已经被打倒的怪物,`0` 表示未被打倒的怪物。
86+
我们定义状态 $mask$ 表示当前已经打怪的情况,其二进制中的 $1$ 表示已经被打倒的怪物,而 $0$ 表示未被打倒的怪物。
8787

88-
时间复杂度 $O(n\times 2^n)$,空间复杂度 $O(2^n)$。其中 $n$ 是怪物数量。
88+
时间复杂度 $O(n \times 2^n)$,空间复杂度 $O(2^n)$。其中 $n$ 是怪物数量。
8989

9090
<!-- tabs:start -->
9191

solution/2400-2499/2403.Minimum Time to Kill All Monsters/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ It can be proven that 6 is the minimum number of days needed.
6969

7070
## Solutions
7171

72+
**Solution 1: State Compression + Memorization Search or Dynamic Programming**
73+
74+
Since defeating monsters can increase the daily magic power gain $gain$, the order of defeating monsters affects the result, so we need to enumerate. Noting that the data range of the problem is small, we consider using state compression dynamic programming to solve it.
75+
76+
We define a state $mask$ to represent the current situation of defeating monsters. In its binary representation, $1$ represents the monsters that have been defeated, and $0$ represents the monsters that have not been defeated.
77+
78+
The time complexity is $O(n \times 2^n)$, and the space complexity is $O(2^n)$. Here, $n$ is the number of monsters.
79+
7280
<!-- tabs:start -->
7381

7482
### **Python3**

solution/2400-2499/2404.Most Frequent Even Element/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ We return the smallest one, which is 2.</pre>
4444

4545
## Solutions
4646

47+
**Solution 1: Hash Table**
48+
49+
We use a hash table $cnt$ to count the occurrence of all even elements, and then find the even element with the highest occurrence and the smallest value.
50+
51+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.
52+
4753
<!-- tabs:start -->
4854

4955
### **Python3**

solution/2400-2499/2405.Optimal Partition of String/README.md

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

5353
过程中,可以用哈希表记录当前子字符串的所有字符,空间复杂度 $O(n)$;也可以使用一个数字,用位运算的方式记录字符,空间复杂度 $O(1)$。
5454

55-
时间复杂度 $O(n)$。其中 $n$ 是字符串 `s` 的长度。
55+
时间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
5656

5757
<!-- tabs:start -->
5858

solution/2400-2499/2405.Optimal Partition of String/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ It can be shown that 4 is the minimum number of substrings needed.
4040

4141
## Solutions
4242

43+
**Solution 1: Greedy**
44+
45+
According to the problem, each substring should be as long as possible and contain unique characters. We just need to partition greedily.
46+
47+
During the process, we can use a hash table to record all characters in the current substring, with a space complexity of $O(n)$; or we can use a number to record characters using bitwise operations, with a space complexity of $O(1)$.
48+
49+
The time complexity is $O(n)$, where $n$ is the length of the string $s$.
50+
4351
<!-- tabs:start -->
4452

4553
### **Python3**

solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@
5151

5252
**方法一:贪心 + 优先队列(小根堆)**
5353

54-
先将区间按左端点排序,用小根堆维护每组的最右端点(堆顶是所有组的最右端点的最小值)。遍历每个区间:
54+
我们先将区间按左端点排序,用小根堆维护每组的最右端点(堆顶是所有组的最右端点的最小值)。
55+
56+
接下来,我们遍历每个区间:
5557

5658
- 若当前区间左端点大于堆顶元素,说明当前区间可以加入到堆顶元素所在的组中,我们直接弹出堆顶元素,然后将当前区间的右端点放入堆中;
5759
- 否则,说明当前没有组能容纳当前区间,那么我们就新建一个组,将当前区间的右端点放入堆中。
5860

59-
时间复杂度 $O(n\log n)$。其中 $n$ 是数组 `intervals` 的长度。
61+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `intervals` 的长度。
6062

6163
<!-- tabs:start -->
6264

0 commit comments

Comments
 (0)