You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0000-0099/0049.Group Anagrams/README_EN.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,7 @@ The time complexity is $O(n\times k\times \log k)$, where $n$ and $k$ are the le
50
50
51
51
**Solution 2: Counting**
52
52
53
-
We can also change the sorting part in Method 1 to counting, that is, use the characters in each string $s$ and their occurrence times as `key`, and use the string $s$ as `value` to store in the hash table.
53
+
We can also change the sorting part in Solution 1 to counting, that is, use the characters in each string $s$ and their occurrence times as `key`, and use the string $s$ as `value` to store in the hash table.
54
54
55
55
The time complexity is $O(n\times (k + C))$, where $n$ and $k$ are the lengths of the string array and the maximum length of the string, respectively, and $C$ is the size of the character set. In this problem, $C = 26$.
Copy file name to clipboardexpand all lines: solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README_EN.md
+12-12
Original file line number
Diff line number
Diff line change
@@ -48,21 +48,21 @@ Total profit is 4.
48
48
49
49
## Solutions
50
50
51
-
**Solution 1: Greedy**
51
+
**Solution 1: Greedy Algorithm**
52
52
53
-
From the second day, if the stock price on that day is greater than the previous day, buy it on the previous day and sell it on that day to get a profit. If the stock price on that day is less than the previous day, do not buy it or sell it. That is to say, all the rising trading days are bought and sold, and all the falling trading days are not bought or sold, and the final profit is the maximum.
53
+
Starting from the second day, if the stock price is higher than the previous day, buy on the previous day and sell on the current day to make a profit. If the stock price is lower than the previous day, do not buy or sell. In other words, buy and sell on all rising trading days, and do not trade on all falling trading days. The final profit will be the maximum.
54
54
55
-
The time complexity is $O(n)$, where $n$ is the length of the array `prices`. The space complexity is $O(1)$.
55
+
The time complexity is $O(n)$, where $n$ is the length of the `prices` array. The space complexity is $O(1)$.
56
56
57
57
**Solution 2: Dynamic Programming**
58
58
59
-
Let $f[i][j]$ represent the maximum profit after the $i$th day of trading, where $j$ represents whether the current stock is held, and $j=0$when the stock is held, and $j=1$ when the stock is not held. The initial state is $f[0][0]=-prices[0]$, and all other states are $0$.
59
+
We define $f[i][j]$ as the maximum profit after trading on the $i$th day, where $j$ indicates whether we currently hold the stock. When holding the stock, $j=0$, and when not holding the stock, $j=1$. The initial state is $f[0][0]=-prices[0]$, and all other states are $0$.
60
60
61
-
If the current stock is held, it may be that the stock was held on the previous day and nothing was done today, that is, $f[i][0]=f[i-1][0]$; or the stock was not held the previous day and the stock was bought today, that is, $f[i][0]=f[i-1][1]-prices[i]$.
61
+
If we currently hold the stock, it may be that we held the stock the day before and do nothing today, i.e., $f[i][0]=f[i-1][0]$. Or it may be that we did not hold the stock the day before and bought the stock today, i.e., $f[i][0]=f[i-1][1]-prices[i]$.
62
62
63
-
If the current stock is not held, it may be that the stock was not held the previous day and nothing was done today, that is, $f[i][1]=f[i-1][1]$; or the stock was held the previous day and the stock was sold today, that is, $f[i][1]=f[i-1][0]+prices[i]$.
63
+
If we currently do not hold the stock, it may be that we did not hold the stock the day before and do nothing today, i.e., $f[i][1]=f[i-1][1]$. Or it may be that we held the stock the day before and sold the stock today, i.e., $f[i][1]=f[i-1][0]+prices[i]$.
64
64
65
-
Therefore, we can write down the state transition equation:
65
+
Therefore, we can write the state transition equation as:
We can find that in Solution 2, the state of the $i$th day only depends on the state of the $i-1$th day, so we can only use two variables to maintain the state of the $i-1$th day. Therefore, the space complexity can be optimized to $O(1)$.
80
+
We can find that in Solution 2, the state of the $i$th day is only related to the state of the $i-1$th day. Therefore, we can use only two variables to maintain the state of the $i-1$th day, thereby optimizing the space complexity to $O(1)$.
81
81
82
-
Time complexity $O(n)$, where $n$ is the length of the array `prices`. Space complexity $O(1)$.
82
+
The time complexity is $O(n)$, where $n$ is the length of the `prices` array. The space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/0100-0199/0123.Best Time to Buy and Sell Stock III/README_EN.md
+15
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,21 @@ Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
46
46
47
47
## Solutions
48
48
49
+
**Solution 1: Dynamic Programming**
50
+
51
+
We define the following variables:
52
+
53
+
-`f1` represents the maximum profit after the first purchase of the stock;
54
+
-`f2` represents the maximum profit after the first sale of the stock;
55
+
-`f3` represents the maximum profit after the second purchase of the stock;
56
+
-`f4` represents the maximum profit after the second sale of the stock.
57
+
58
+
During the traversal, we directly calculate `f1`, `f2`, `f3`, `f4`. We consider that buying and selling on the same day will result in a profit of $0$, which will not affect the answer.
59
+
60
+
Finally, return `f4`.
61
+
62
+
The time complexity is $O(n)$, where $n$ is the length of the `prices` array. The space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/0100-0199/0124.Binary Tree Maximum Path Sum/README_EN.md
+7-7
Original file line number
Diff line number
Diff line change
@@ -39,25 +39,25 @@
39
39
40
40
**Solution 1: Recursion**
41
41
42
-
We think about the classic routine of binary tree recursion problem:
42
+
When thinking about the classic routine of recursion problems in binary trees, we consider:
43
43
44
44
1. Termination condition (when to terminate recursion)
45
-
2.Recursion processing left and right subtrees
46
-
3.Combine the calculation results of the left and right subtrees
45
+
2.Recursively process the left and right subtrees
46
+
3.Merge the calculation results of the left and right subtrees
47
47
48
-
For this topic, we design a function $dfs(root)$, which returns the maximum path sum of the binary tree with $root$ as the root node.
48
+
For this problem, we design a function $dfs(root)$, which returns the maximum path sum of the binary tree with $root$ as the root node.
49
49
50
50
The execution logic of the function $dfs(root)$ is as follows:
51
51
52
52
If $root$ does not exist, then $dfs(root)$ returns $0$;
53
53
54
-
Otherwise, we recursively calculate the maximum path sum of the left subtree and the right subtree of $root$, denoted as $left$ and $right$, respectively. If $left$ is less than $0$, then we set it to $0$, similarly, if $right$ is less than $0$, then we set it to $0$.
54
+
Otherwise, we recursively calculate the maximum path sum of the left and right subtrees of $root$, denoted as $left$ and $right$. If $left$ is less than $0$, then we set it to $0$, similarly, if $right$ is less than $0$, then we set it to $0$.
55
55
56
56
Then, we update the answer with $root.val + left + right$. Finally, the function returns $root.val + \max(left, right)$.
57
57
58
-
In the main function, we call $dfs(root)$ to get the maximum path sum of each node, and the maximum value in it is the answer.
58
+
In the main function, we call $dfs(root)$ to get the maximum path sum of each node, and the maximum value among them is the answer.
59
59
60
-
Time complexity $O(n)$, space complexity $O(n)$. Where $n$ is the number of nodes in the binary tree.
60
+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
Copy file name to clipboardexpand all lines: solution/0100-0199/0127.Word Ladder/README_EN.md
+35-1
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,41 @@
46
46
47
47
## Solutions
48
48
49
-
BFS.
49
+
**Solution 1: BFS**
50
+
51
+
BFS minimum step model. This problem can be solved with naive BFS, or it can be optimized with bidirectional BFS to reduce the search space and improve efficiency.
52
+
53
+
Bidirectional BFS is a common optimization method for BFS, with the main implementation ideas as follows:
54
+
55
+
1. Create two queues, q1 and q2, for "start -> end" and "end -> start" search directions, respectively.
56
+
2. Create two hash maps, m1 and m2, to record the visited nodes and their corresponding expansion times (steps).
57
+
3. During each search, prioritize the queue with fewer elements for search expansion. If a node visited from the other direction is found during the expansion, it means the shortest path has been found.
58
+
4. If one of the queues is empty, it means that the search in the current direction cannot continue, indicating that the start and end points are not connected, and there is no need to continue the search.
59
+
60
+
```python
61
+
while q1 and q2:
62
+
iflen(q1) <=len(q2):
63
+
# Prioritize the queue with fewer elements for expansion
64
+
extend(m1, m2, q1)
65
+
else:
66
+
extend(m2, m1, q2)
67
+
68
+
69
+
defextend(m1, m2, q):
70
+
# New round of expansion
71
+
for _ inrange(len(q)):
72
+
p = q.popleft()
73
+
step = m1[p]
74
+
for t innext(p):
75
+
if t in m1:
76
+
# Already visited before
77
+
continue
78
+
if t in m2:
79
+
# The other direction has been searched, indicating that a shortest path has been found
Copy file name to clipboardexpand all lines: solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md
+8-8
Original file line number
Diff line number
Diff line change
@@ -36,23 +36,23 @@
36
36
37
37
**Solution 1: Sorting**
38
38
39
-
First, we sort the array, and use a variable $t$ to record the length of the current continuous sequence, and use a variable $ans$ to record the length of the longest continuous sequence.
39
+
First, we sort the array, then use a variable $t$ to record the current length of the consecutive sequence, and a variable $ans$ to record the length of the longest consecutive sequence.
40
40
41
-
Then, we start to traverse the array from index $i=1$, for the current element $nums[i]$:
41
+
Next, we start traversing the array from index $i=1$. For the current element $nums[i]$:
42
42
43
-
- If $nums[i]=nums[i-1]$ , then the current element is duplicate, and we don't need to consider it;
44
-
- If $nums[i]=nums[i-1]+1$ , then the current element can be connected to the previous continuous sequence to form a longer continuous sequence, so we update $t = t + 1$ and update the answer $ans = \max(ans, t)$;
45
-
- Otherwise, the current element cannot be connected to the previous continuous sequence, so we reset $t$ to $1$.
43
+
- If $nums[i] = nums[i-1]$, it means the current element is repeated and does not need to be considered.
44
+
- If $nums[i] = nums[i-1] + 1$, it means the current element can be appended to the previous consecutive sequence to form a longer consecutive sequence. We update $t = t + 1$, and then update the answer $ans = \max(ans, t)$.
45
+
- Otherwise, it means the current element cannot be appended to the previous consecutive sequence, and we reset $t$ to $1$.
46
46
47
47
Finally, we return the answer $ans$.
48
48
49
-
Time complexity $O(n \times \log n)$, space complexity $O(\log n)$, where $n$ is the length of the array.
49
+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array.
50
50
51
51
**Solution 2: Hash Table**
52
52
53
-
We use a hash table to store all the elements in the array, and then we traverse the array to find the longest continuous sequence for each element $x$. If the predecessor of the current element $x-1$ is not in the hash table, we use the current element as the starting point, and keep trying to match $x+1, x+2, x+3, \dots$ until we cannot match, the matching length at this time is the length of the longest continuous sequence starting from $x$, so we update the answer.
53
+
We use a hash table to store all elements in the array, and then traverse each element $x$ in the array. If the predecessor $x-1$ of the current element is not in the hash table, then we start with the current element and continuously try to match $x+1, x+2, x+3, \dots$, until no match is found. The length of the match at this time is the longest consecutive sequence length starting with $x$, and we update the answer accordingly.
54
54
55
-
Time complexity $O(n)$, space complexity $O(n)$, where $n$ is the length of the array.
55
+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.
We can design a function $dfs(root, s)$, which represents the sum of all path numbers from the current node $root$ to the leaf nodes, given that the current path number is $s$. The answer is $dfs(root, 0)$.
57
+
58
+
The calculation of the function $dfs(root, s)$ is as follows:
59
+
60
+
- If the current node $root$ is null, return $0$.
61
+
- Otherwise, add the value of the current node to $s$, i.e., $s = s \times 10 + root.val$.
0 commit comments