Skip to content

Commit e13866d

Browse files
authored
feat: add solutions to lc problems (doocs#2029)
1 parent f5d3f05 commit e13866d

File tree

116 files changed

+1510
-391
lines changed

Some content is hidden

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

116 files changed

+1510
-391
lines changed

solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is
6161

6262
**Solution 2: Space Optimization**
6363

64-
The space complexity of Method 1 is relatively high because it requires a queue to store the nodes of each level. We can implement it with constant space.
64+
The space complexity of Solution 1 is relatively high because it requires a queue to store the nodes of each level. We can implement it with constant space.
6565

6666
We define two pointers $prev$ and $next$, which point to the previous node and the first node of the next level, respectively. When traversing the nodes of the current level, we string the nodes of the next level together and find the first node of the next level. After the current level is traversed, we assign the first node $next$ of the next level to $node$ and continue to traverse.
6767

solution/0200-0299/0207.Course Schedule/README_EN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ To take course 1 you should have finished course 0, and to take course 0 you sho
4444

4545
## Solutions
4646

47+
**Solution 1: Topological Sorting**
48+
49+
For this problem, we can consider the courses as nodes in a graph, and prerequisites as edges in the graph. Thus, we can transform this problem into determining whether there is a cycle in the directed graph.
50+
51+
Specifically, we can use the idea of topological sorting. For each node with an in-degree of $0$, we reduce the in-degree of its out-degree nodes by $1$, until all nodes have been traversed.
52+
53+
If all nodes have been traversed, it means there is no cycle in the graph, and we can complete all courses; otherwise, we cannot complete all courses.
54+
55+
The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Here, $n$ and $m$ are the number of courses and prerequisites respectively.
56+
4757
<!-- tabs:start -->
4858

4959
### **Python3**

solution/0200-0299/0209.Minimum Size Subarray Sum/README.md

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

7171
我们可以使用双指针 $j$ 和 $i$ 维护一个窗口,其中窗口中的所有元素之和小于 $target$。初始时 $j = 0$,答案 $ans = n + 1$,其中 $n$ 为数组 $nums$ 的长度。
7272

73-
接下来,指针 $i$ 从 $0$ 开始向右移动,每次移动一步,我们将指针 $i$ 对应的元素加入窗口,同时更新窗口中元素之和。如果窗口中元素之和大于等于 $target$,说明当前子数组满足条件,我们可以更新答案,即 $ans = min(ans, i - j + 1)$。然后我们不断地从窗口中移除元素 $nums[j]$,直到窗口中元素之和小于 $target$,然后重复上述过程。
73+
接下来,指针 $i$ 从 $0$ 开始向右移动,每次移动一步,我们将指针 $i$ 对应的元素加入窗口,同时更新窗口中元素之和。如果窗口中元素之和大于等于 $target$,说明当前子数组满足条件,我们可以更新答案,即 $ans = \min(ans, i - j + 1)$。然后我们不断地从窗口中移除元素 $nums[j]$,直到窗口中元素之和小于 $target$,然后重复上述过程。
7474

7575
最后,如果 $ans \leq n$,则说明存在满足条件的子数组,返回 $ans$,否则返回 $0$。
7676

solution/0200-0299/0209.Minimum Size Subarray Sum/README_EN.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,25 @@
4343

4444
## Solutions
4545

46-
**Method 1: PreSum & Binary search**
46+
**Solution 1: Prefix Sum + Binary Search**
4747

48-
**Method 2: Slide window**
48+
First, we preprocess the prefix sum array $s$ of the array $nums$, where $s[i]$ represents the sum of the first $i$ elements of the array $nums$. Since all elements in the array $nums$ are positive integers, the array $s$ is also monotonically increasing. Also, we initialize the answer $ans = n + 1$, where $n$ is the length of the array $nums$.
49+
50+
Next, we traverse the prefix sum array $s$. For each element $s[i]$, we can find the smallest index $j$ that satisfies $s[j] \geq s[i] + target$ by binary search. If $j \leq n$, it means that there exists a subarray that satisfies the condition, and we can update the answer, i.e., $ans = min(ans, j - i)$.
51+
52+
Finally, if $ans \leq n$, it means that there exists a subarray that satisfies the condition, return $ans$, otherwise return $0$.
53+
54+
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$.
55+
56+
**Solution 2: Two Pointers**
57+
58+
We can use two pointers $j$ and $i$ to maintain a window, where the sum of all elements in the window is less than $target$. Initially, $j = 0$, and the answer $ans = n + 1$, where $n$ is the length of the array $nums$.
59+
60+
Next, the pointer $i$ starts to move to the right from $0$, moving one step each time. We add the element corresponding to the pointer $i$ to the window and update the sum of the elements in the window. If the sum of the elements in the window is greater than or equal to $target$, it means that the current subarray satisfies the condition, and we can update the answer, i.e., $ans = \min(ans, i - j + 1)$. Then we continuously remove the element $nums[j]$ from the window until the sum of the elements in the window is less than $target$, and then repeat the above process.
61+
62+
Finally, if $ans \leq n$, it means that there exists a subarray that satisfies the condition, return $ans$, otherwise return $0$.
63+
64+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $nums$.
4965

5066
<!-- tabs:start -->
5167

solution/0300-0399/0367.Valid Perfect Square/README_EN.md

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

3737
## Solutions
3838

39-
**Method 1: Binary search**
39+
**Solution 1: Binary search**
4040

41-
**Method 2: Math trick**
41+
**Solution 2: Math trick**
4242

4343
This is a math problem:
4444

solution/0600-0699/0619.Biggest Single Number/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ We can first group the `MyNumbers` table by `num` and count the number of occurr
8585

8686
**Solution 2: Grouping and `CASE` Expression**
8787

88-
Similar to Method 1, we can first group the `MyNumbers` table by `num` and count the number of occurrences of each number. Then, we can use a `CASE` expression to find the numbers that appear only once, sort them in descending order by number, and take the first one.
88+
Similar to Solution 1, we can first group the `MyNumbers` table by `num` and count the number of occurrences of each number. Then, we can use a `CASE` expression to find the numbers that appear only once, sort them in descending order by number, and take the first one.
8989

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

solution/0600-0699/0658.Find K Closest Elements/README_EN.md

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

3434
## Solutions
3535

36-
**Method 1: Sort**
36+
**Solution 1: Sort**
3737

38-
**Method 2: Binary search**
38+
**Solution 2: Binary search**
3939

4040
<!-- tabs:start -->
4141

solution/1000-1099/1099.Two Sum Less Than K/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log
4646

4747
**Solution 2: Sorting + Two Pointers**
4848

49-
Similar to Method 1, we can first sort the array $nums$, and initialize the answer as $-1$.
49+
Similar to Solution 1, we can first sort the array $nums$, and initialize the answer as $-1$.
5050

5151
Next, we use two pointers $i$ and $j$ to point to the left and right ends of the array, respectively. Each time we judge whether $s = nums[i] + nums[j]$ is less than $k$. If it is less than $k$, then we can update the answer, i.e., $ans = \max(ans, s)$, and move $i$ one step to the right, otherwise move $j$ one step to the left.
5252

solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README_EN.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ Note that nodes 6, 0, and 8 are also leaf nodes, but the depth of them is 2, but
5454

5555
## Solutions
5656

57+
**Solution 1: DFS**
58+
59+
We design a function `dfs(root)` that returns a tuple `(l, d)`, where `l` is the deepest common ancestor of node `root`, and `d` is the depth of node `root`. The execution logic of the function `dfs(root)` is as follows:
60+
61+
- If `root` is null, return the tuple `(None, 0)`;
62+
- Otherwise, we recursively call `dfs(root.left)` and `dfs(root.right)`, obtaining tuples `(l, d1)` and `(r, d2)`. If `d1 > d2`, the deepest common ancestor of `root` is `l`, and the depth is `d1 + 1`; if `d1 < d2`, the deepest common ancestor of `root` is `r`, and the depth is `d2 + 1`; if `d1 = d2`, the deepest common ancestor of `root` is `root`, and the depth is `d1 + 1`.
63+
64+
In the main function, we call `dfs(root)` and return the first element of its return value to get the deepest common ancestor node.
65+
66+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
67+
5768
<!-- tabs:start -->
5869

5970
### **Python3**

solution/1100-1199/1124.Longest Well-Performing Interval/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
接下来,我们遍历数组 `hours`,对于每个下标 $i$:
5151

5252
- 如果 $hours[i] \gt 8$,我们就让 $s$ 加 $1$,否则减 $1$。
53-
- 如果 $s$ 大于 $0$,说明从下标 $0$ 到当前下标的这一段,满足「表现良好的时间段」,我们更新结果 $ans = i + 1$。否则,如果 $s - 1$ 在哈希表 $pos$ 中,记 $j = pos[s - 1]$,说明从下标 $j + 1$ 到当前下标 $i$ 的这一段,满足「表现良好的时间段」,我们更新结果 $ans = max(ans, i - j)$。
53+
- 如果 $s$ 大于 $0$,说明从下标 $0$ 到当前下标的这一段,满足「表现良好的时间段」,我们更新结果 $ans = i + 1$。否则,如果 $s - 1$ 在哈希表 $pos$ 中,记 $j = pos[s - 1]$,说明从下标 $j + 1$ 到当前下标 $i$ 的这一段,满足「表现良好的时间段」,我们更新结果 $ans = \max(ans, i - j)$。
5454
- 然后,如果 $s$ 不在哈希表 $pos$ 中,我们就记录 $pos[s] = i$。
5555

5656
遍历结束后,返回答案即可。

0 commit comments

Comments
 (0)