Skip to content

Commit 7c6b9bb

Browse files
authored
feat: add solutions to lc problems: No.200+ (doocs#2180)
1 parent e32f004 commit 7c6b9bb

File tree

13 files changed

+138
-8
lines changed

13 files changed

+138
-8
lines changed

solution/0200-0299/0213.House Robber II/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ Total amount you can rob = 1 + 3 = 4.
4343

4444
## Solutions
4545

46+
**Solution 1: Dynamic Programming**
47+
48+
The circular arrangement means that at most one of the first and last houses can be chosen for theft, so this circular arrangement problem can be reduced to two single-row house problems.
49+
50+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
51+
4652
<!-- tabs:start -->
4753

4854
### **Python3**

solution/0200-0299/0215.Kth Largest Element in an Array/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@
4242

4343
**方法一:排序**
4444

45-
将数组 $nums$ 升序排列,然后获取 $nums[n-k]$。
45+
我们可以将数组 $nums$ 升序排列,然后获取 $nums[n-k]$。
4646

47-
时间复杂度 $O(nlogn)$,其中 $n$ 表示数组 $nums$ 的长度。
47+
时间复杂度 $O(n \times \log n)$,其中 $n$ 表示数组 $nums$ 的长度。
4848

49-
**方法二:partition**
49+
**方法二:Partition**
5050

51-
并不是所有时候,都需要整个数组进入有序状态,只需要**局部有序**,或者说,从大到小排序,只要 $[0..k)$ 位置的元素有序,那么就能确定结果,此处使用**快速排序**
51+
我们注意到,并不是所有时候,都需要整个数组进入有序状态,只需要**局部有序**,或者说,从大到小排序,只要 $[0..k)$ 位置的元素有序,那么就能确定结果,此处使用**快速排序**
5252

53-
快速排序有一特点,每一次循环结束时,能够确定的是$partition$ 一定处于它该处于的索引位置。从而根据它得知,结果值是在左数组还是在右数组当中,然后对那一数组进行排序即可。
53+
快速排序有一特点,每一次循环结束时,能够确定的是 $partition$ 一定处于它该处于的索引位置。从而根据它得知,结果值是在左数组还是在右数组当中,然后对那一数组进行排序即可。
5454

5555
时间复杂度 $O(n)$,其中 $n$ 表示数组 $nums$ 的长度。
5656

solution/0200-0299/0215.Kth Largest Element in an Array/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@
2828

2929
## Solutions
3030

31+
**Solution 1: Sorting**
32+
33+
We can sort the array $nums$ in ascending order, and then get $nums[n-k]$.
34+
35+
The time complexity is $O(n \times \log n)$, where $n$ is the length of the array $nums$.
36+
37+
**Solution 2: Partition**
38+
39+
We notice that it is not always necessary for the entire array to be in an ordered state. We only need **local order**. That is to say, if the elements in the position $[0..k)$ are sorted in descending order, then we can determine the result. Here we use **quick sort**.
40+
41+
Quick sort has a characteristic that at the end of each loop, it can be determined that the $partition$ is definitely at the index position it should be. Therefore, based on it, we know whether the result value is in the left array or in the right array, and then sort that array.
42+
43+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$.
44+
3145
<!-- tabs:start -->
3246

3347
### **Python3**

solution/0200-0299/0217.Contains Duplicate/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@
2727

2828
## Solutions
2929

30+
**Solution 1: Sorting**
31+
32+
First, we sort the array `nums`.
33+
34+
Then, we traverse the array. If there are two adjacent elements that are the same, it means that there are duplicate elements in the array, and we directly return `true`.
35+
36+
Otherwise, when the traversal ends, we return `false`.
37+
38+
The time complexity is $O(n \times \log n)$, where $n$ is the length of the array `nums`.
39+
40+
**Solution 2: Hash Table**
41+
42+
We traverse the array and record the elements that have appeared in the hash table $s$. If an element appears for the second time, it means that there are duplicate elements in the array, and we directly return `true`.
43+
44+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array `nums`.
45+
3046
<!-- tabs:start -->
3147

3248
### **Python3**

solution/0200-0299/0220.Contains Duplicate III/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0
5757

5858
**方法一:滑动窗口 + 有序集合**
5959

60-
维护一个大小为 $k$ 的滑动窗口,窗口中的元素保持有序。
60+
我们维护一个大小为 $k$ 的滑动窗口,窗口中的元素保持有序。
6161

6262
遍历数组 `nums`,对于每个元素 $nums[i]$,我们在有序集合中查找第一个大于等于 $nums[i] - t$ 的元素,如果元素存在,并且该元素小于等于 $nums[i] + t$,说明找到了一对符合条件的元素,返回 `true`。否则,我们将 $nums[i]$ 插入到有序集合中,并且如果有序集合的大小超过了 $k$,我们需要将最早加入有序集合的元素删除。
6363

64-
时间复杂度 $O(n\times \log k)$,其中 $n$ 是数组 `nums` 的长度。对于每个元素,我们需要 $O(\log k)$ 的时间来查找有序集合中的元素,一共有 $n$ 个元素,因此总时间复杂度是 $O(n\times \log k)$。
64+
时间复杂度 $O(n \times \log k)$,其中 $n$ 是数组 `nums` 的长度。对于每个元素,我们需要 $O(\log k)$ 的时间来查找有序集合中的元素,一共有 $n$ 个元素,因此总时间复杂度是 $O(n \times \log k)$。
6565

6666
<!-- tabs:start -->
6767

solution/0200-0299/0220.Contains Duplicate III/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0
4949

5050
## Solutions
5151

52+
**Solution 1: Sliding Window + Ordered Set**
53+
54+
We maintain a sliding window of size $k$, and the elements in the window are kept in order.
55+
56+
We traverse the array `nums`. For each element $nums[i]$, we look for the first element in the ordered set that is greater than or equal to $nums[i] - t$. If the element exists, and this element is less than or equal to $nums[i] + t$, it means we have found a pair of elements that meet the conditions, and we return `true`. Otherwise, we insert $nums[i]$ into the ordered set, and if the size of the ordered set exceeds $k$, we need to remove the earliest added element from the ordered set.
57+
58+
The time complexity is $O(n \times \log k)$, where $n$ is the length of the array `nums`. For each element, we need $O(\log k)$ time to find the element in the ordered set, and there are $n$ elements in total, so the total time complexity is $O(n \times \log k)$.
59+
5260
<!-- tabs:start -->
5361

5462
### **Python3**

solution/0200-0299/0221.Maximal Square/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@
4040

4141
## Solutions
4242

43+
**Solution 1: Dynamic Programming**
44+
45+
We define $dp[i + 1][j + 1]$ as the maximum square side length with the lower right corner at index $(i, j)$. The answer is the maximum value among all $dp[i + 1][j + 1]$.
46+
47+
The state transition equation is:
48+
49+
$$
50+
dp[i + 1][j + 1] =
51+
\begin{cases}
52+
0 & \text{if } matrix[i][j] = '0' \\
53+
\min(dp[i][j], dp[i][j + 1], dp[i + 1][j]) + 1 & \text{if } matrix[i][j] = '1'
54+
\end{cases}
55+
$$
56+
57+
The time complexity is $O(m\times n)$, and the space complexity is $O(m\times n)$. Where $m$ and $n$ are the number of rows and columns of the matrix, respectively.
58+
4359
<!-- tabs:start -->
4460

4561
### **Python3**

solution/0200-0299/0222.Count Complete Tree Nodes/README_EN.md

+21
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Recursion**
47+
48+
We recursively traverse the entire tree and count the number of nodes.
49+
50+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the tree.
51+
52+
**Solution 2: Binary Search**
53+
54+
For this problem, we can also take advantage of the characteristics of a complete binary tree to design a faster algorithm.
55+
56+
Characteristics of a complete binary tree: leaf nodes can only appear on the bottom and second-to-bottom layers, and the leaf nodes on the bottom layer are concentrated on the left side of the tree. It should be noted that a full binary tree is definitely a complete binary tree, but a complete binary tree is not necessarily a full binary tree.
57+
58+
If the number of layers in a full binary tree is $h$, then the total number of nodes is $2^h - 1$.
59+
60+
We first count the heights of the left and right subtrees of $root$, denoted as $left$ and $right$.
61+
62+
1. If $left = right$, it means that the left subtree is a full binary tree, so the total number of nodes in the left subtree is $2^{left} - 1$. Plus the $root$ node, it is $2^{left}$. Then we recursively count the right subtree.
63+
1. If $left > right$, it means that the right subtree is a full binary tree, so the total number of nodes in the right subtree is $2^{right} - 1$. Plus the $root$ node, it is $2^{right}$. Then we recursively count the left subtree.
64+
65+
The time complexity is $O(\log^2 n)$.
66+
4667
<!-- tabs:start -->
4768

4869
### **Python3**

solution/0200-0299/0223.Rectangle Area/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737

3838
## Solutions
3939

40+
**Solution 1: Calculate Overlapping Area**
41+
42+
First, we calculate the area of the two rectangles separately, denoted as $a$ and $b$. Then we calculate the overlapping width $width$ and height $height$. The overlapping area is $max(width, 0) \times max(height, 0)$. Finally, we subtract the overlapping area from $a$ and $b$.
43+
44+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
45+
4046
<!-- tabs:start -->
4147

4248
### **Python3**

solution/0200-0299/0224.Basic Calculator/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@
4545

4646
## Solutions
4747

48+
**Solution 1: Stack**
49+
50+
We use a stack $stk$ to save the current calculation result and operator, a variable $sign$ to save the current sign, and a variable $ans$ to save the final calculation result.
51+
52+
Next, we traverse each character of the string $s$:
53+
54+
- If the current character is a number, we use a loop to read the following consecutive numbers, and then add or subtract it to $ans$ according to the current sign.
55+
- If the current character is `'+'`, we change the variable $sign$ to positive.
56+
- If the current character is `'-'`, we change the variable $sign$ to negative.
57+
- If the current character is `'('`, we push the current $ans$ and $sign$ into the stack, and reset them to empty and 1, and start to calculate the new $ans$ and $sign$.
58+
- If the current character is `')'`, we pop the top two elements of the stack, one is the operator, and the other is the number calculated before the bracket. We multiply the current number by the operator, and add the previous number to get the new $ans$.
59+
60+
After traversing the string $s$, we return $ans$.
61+
62+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.
63+
4864
<!-- tabs:start -->
4965

5066
### **Python3**

solution/0200-0299/0225.Implement Stack using Queues/README_EN.md

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ myStack.empty(); // return False
5555

5656
## Solutions
5757

58+
**Solution 1: Two Queues**
59+
60+
We use two queues $q_1$ and $q_2$, where $q_1$ is used to store the elements in the stack, and $q_2$ is used to assist in implementing the stack operations.
61+
62+
- `push` operation: Push the element into $q_2$, then pop the elements in $q_1$ one by one and push them into $q_2$, finally swap the references of $q_1$ and $q_2$. The time complexity is $O(n)$.
63+
- `pop` operation: Directly pop the front element of $q_1$. The time complexity is $O(1)$.
64+
- `top` operation: Directly return the front element of $q_1$. The time complexity is $O(1)$.
65+
- `empty` operation: Check whether $q_1$ is empty. The time complexity is $O(1)$.
66+
67+
The space complexity is $O(n)$, where $n$ is the number of elements in the stack.
68+
5869
<!-- tabs:start -->
5970

6071
### **Python3**

solution/0200-0299/0226.Invert Binary Tree/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838

3939
## Solutions
4040

41-
DFS.
41+
**Solution 1: Recursion**
42+
43+
The idea of recursion is very simple, which is to swap the left and right subtrees of the current node, and then recursively swap the left and right subtrees of the current node.
44+
45+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the binary tree.
4246

4347
<!-- tabs:start -->
4448

solution/0200-0299/0227.Basic Calculator II/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@
3636

3737
## Solutions
3838

39+
**Solution 1: Stack**
40+
41+
We traverse the string $s$, and use a variable `sign` to record the operator before each number. For the first number, its previous operator is considered as a plus sign. Each time we traverse to the end of a number, we decide the calculation method based on `sign`:
42+
43+
- Plus sign: push the number into the stack;
44+
- Minus sign: push the opposite number into the stack;
45+
- Multiplication and division signs: calculate the number with the top element of the stack, and replace the top element of the stack with the calculation result.
46+
47+
After the traversal ends, the sum of the elements in the stack is the answer.
48+
49+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.
50+
3951
<!-- tabs:start -->
4052

4153
### **Python3**

0 commit comments

Comments
 (0)