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: lcci/04.12.Paths with Sum/README_EN.md
+18Lines changed: 18 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,24 @@ Given the following tree and <code>sum = 22,</code></p>
44
44
45
45
## Solutions
46
46
47
+
**Solution 1: Hash Table + Prefix Sum + Recursion**
48
+
49
+
We can use the idea of prefix sum to recursively traverse the binary tree, and use a hash table $cnt$ to count the occurrence of each prefix sum on the path from the root node to the current node.
50
+
51
+
We design a recursive function $dfs(node, s),ドル where the current node being traversed is $node,ドル and the prefix sum on the path from the root node to the current node is $s$. The return value of the function is the number of paths with the path sum equal to $sum$ and the path ends at the $node$ node or its subtree nodes. Therefore, the answer is $dfs(root, 0)$.
52
+
53
+
The recursive process of the function $dfs(node, s)$ is as follows:
54
+
55
+
- If the current node $node$ is null, return 0ドル$.
56
+
- Calculate the prefix sum $s$ on the path from the root node to the current node.
57
+
- Use $cnt[s - sum]$ to represent the number of paths with the path sum equal to $sum$ and the path ends at the current node, where $cnt[s - sum]$ is the count of the prefix sum equal to $s - sum$ in $cnt$.
58
+
- Add the count of the prefix sum $s$ by 1ドル,ドル i.e., $cnt[s] = cnt[s] + 1$.
59
+
- Recursively traverse the left and right child nodes of the current node, i.e., call the functions $dfs(node.left, s)$ and $dfs(node.right, s),ドル and add their return values.
60
+
- After the return value is calculated, subtract the count of the prefix sum $s$ of the current node by 1ドル,ドル i.e., execute $cnt[s] = cnt[s] - 1$.
61
+
- Finally, return the answer.
62
+
63
+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
64
+
47
65
<!-- tabs:start -->
48
66
49
67
### **Python3**
Collapse file: lcci/05.01.Insert Into Bits/README_EN.md
Copy file name to clipboardExpand all lines: lcci/05.01.Insert Into Bits/README_EN.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,12 @@
24
24
25
25
## Solutions
26
26
27
+
**Solution 1: Bit Manipulation**
28
+
29
+
First, we clear the bits from the $i$-th to the $j$-th in $N,ドル then we left shift $M$ by $i$ bits, and finally perform a bitwise OR operation on $M$ and $N$.
30
+
31
+
The time complexity is $O(\log n),ドル where $n$ is the size of $N$. The space complexity is $O(1)$.
32
+
27
33
<!-- tabs:start -->
28
34
29
35
### **Python3**
Collapse file: lcci/05.02.Binary Number to String/README_EN.md
**Solution 1: Decimal Fraction to Binary Fraction**
34
+
35
+
The method of converting a decimal fraction to a binary fraction is as follows: multiply the decimal part by 2ドル,ドル take the integer part as the next digit of the binary fraction, and take the decimal part as the multiplicand for the next multiplication, until the decimal part is 0ドル$ or the length of the binary fraction exceeds 32ドル$ bits.
36
+
37
+
Let's take an example, suppose we want to convert 0ドル.8125$ to a binary fraction, the process is as follows:
So the binary fraction representation of the decimal fraction 0ドル.8125$ is 0ドル.1101_{(2)}$.
49
+
50
+
For this problem, since the real number is between 0ドル$ and 1ドル,ドル its integer part must be 0ドル$. We only need to convert the decimal part into a binary fraction according to the above method. Stop the conversion when the decimal part is 0ドル$ or the length of the binary fraction is not less than 32ドル$ bits.
51
+
52
+
Finally, if the decimal part is not 0ドル,ドル it means that the real number cannot be represented in binary within 32ドル$ bits, return the string `"ERROR"`. Otherwise, return the converted binary fraction.
53
+
54
+
The time complexity is $O(C),ドル and the space complexity is $O(C)$. Here, $C$ is the length of the binary fraction, with a maximum of 32ドル$.
Copy file name to clipboardExpand all lines: lcci/05.03.Reverse Bits/README_EN.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,14 @@
24
24
25
25
## Solutions
26
26
27
+
**Solution 1: Two Pointers**
28
+
29
+
We can use two pointers $i$ and $j$ to maintain a sliding window, where $i$ is the right pointer and $j$ is the left pointer. Each time the right pointer $i$ moves one bit to the right, if the number of 0ドル$s in the window exceeds 1ドル,ドル then the left pointer $j$ moves one bit to the right, until the number of 0ドル$s in the window does not exceed 1ドル$. Then calculate the length of the window at this time, compare it with the current maximum length, and take the larger value as the current maximum length.
30
+
31
+
Finally, return the maximum length.
32
+
33
+
The time complexity is $O(\log M),ドル and the space complexity is $O(1)$. Here, $M$ is the maximum value of a 32-bit integer.
Copy file name to clipboardExpand all lines: lcci/05.04.Closed Number/README_EN.md
+14Lines changed: 14 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,20 @@
29
29
30
30
## Solutions
31
31
32
+
**Solution 1: Bit Manipulation**
33
+
34
+
First, let's consider how to find the first number that is larger than $num$ and has the same number of 1ドル$s in its binary representation.
35
+
36
+
We can traverse the adjacent two binary bits of $num$ from low to high. If the lower bit is 1ドル$ and the adjacent higher bit is 0ドル,ドル then we have found a position where we can change the 0ドル$ at this position to 1ドル$ and change the 1ドル$ at this position to 0ドル$. Then we move all the remaining lower bits of 1ドル$ to the lowest bit, so we get a number that is larger than $num$ and has the same number of 1ドル$s in its binary representation.
37
+
38
+
Similarly, we can find the first number that is smaller than $num$ and has the same number of 1ドル$s in its binary representation.
39
+
40
+
We can traverse the adjacent two binary bits of $num$ from low to high. If the lower bit is 0ドル$ and the adjacent higher bit is 1ドル,ドル then we have found a position where we can change the 1ドル$ at this position to 0ドル$ and change the 0ドル$ at this position to 1ドル$. Then we move all the remaining lower bits of 0ドル$ to the lowest bit, so we get a number that is smaller than $num$ and has the same number of 1ドル$s in its binary representation.
41
+
42
+
In implementation, we can use a piece of code to handle the above two situations uniformly.
43
+
44
+
The time complexity is $O(\log n),ドル where $n$ is the size of $num$. The space complexity is $O(1)$.
Copy file name to clipboardExpand all lines: lcci/08.01.Three Steps Problem/README_EN.md
+36Lines changed: 36 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,42 @@
32
32
33
33
## Solutions
34
34
35
+
**Solution 1: Recursion**
36
+
37
+
We define $f[i]$ as the number of ways to reach the $i$-th step, initially $f[1]=1,ドル $f[2]=2,ドル $f[3]=4$. The answer is $f[n]$.
38
+
39
+
The recursion formula is $f[i] = f[i-1] + f[i-2] + f[i-3]$.
40
+
41
+
Since $f[i]$ is only related to $f[i-1],ドル $f[i-2],ドル $f[i-3],ドル we can use three variables $a,ドル $b,ドル $c$ to store the values of $f[i-1],ドル $f[i-2],ドル $f[i-3],ドル reducing the space complexity to $O(1)$.
42
+
43
+
The time complexity is $O(n),ドル where $n$ is the given integer. The space complexity is $O(1)$.
44
+
45
+
**Solution 2: Matrix Quick Power to Accelerate Recursion**
46
+
47
+
We set $F(n)$ to represent a 1ドル \times 3$ matrix $\begin{bmatrix} F_{n - 1} & F_{n - 2} & F_{n - 3} \end{bmatrix},ドル where $F_{n - 1},ドル $F_{n - 2}$ and $F_{n - 3}$ respectively represent the number of ways to reach the $n - 1$-th, $n - 2$-th and $n - 3$-th steps.
48
+
49
+
We hope to derive $F(n)$ based on $F(n-1) = \begin{bmatrix} F_{n - 2} & F_{n - 3} & F_{n - 4} \end{bmatrix}$. That is to say, we need a matrix $base,ドル so that $F(n - 1) \times base = F(n),ドル i.e.:
Since $F_n = F_{n - 1} + F_{n - 2} + F_{n - 3},ドル the matrix $base$ is:
58
+
59
+
$$
60
+
\begin{bmatrix}
61
+
1 & 1 & 0 \\
62
+
1 & 0 & 1 \\
63
+
1 & 0 & 0
64
+
\end{bmatrix}
65
+
$$
66
+
67
+
We define the initial matrix $res = \begin{bmatrix} 1 & 1 & 0 \end{bmatrix},ドル then $F_n$ equals the sum of all elements in the result matrix of $res$ multiplied by $base^{n - 4}$. It can be solved using matrix quick power.
68
+
69
+
The time complexity is $O(\log n),ドル and the space complexity is $O(1)$.
70
+
35
71
<!-- tabs:start -->
36
72
37
73
### **Python3**
Collapse file: lcci/08.02.Robot in a Grid/README_EN.md
Copy file name to clipboardExpand all lines: lcci/08.02.Robot in a Grid/README_EN.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,14 @@
32
32
33
33
## Solutions
34
34
35
+
**Solution 1: DFS (Depth-First Search)**
36
+
37
+
We can use depth-first search to solve this problem. We start from the top left corner and move right or down until we reach the bottom right corner. If at some step, we find that the current position is an obstacle, or the current position is already in the path, then we return. Otherwise, we add the current position to the path and mark the current position as visited, then continue to move right or down.
38
+
39
+
If we can finally reach the bottom right corner, then we have found a feasible path, otherwise, it means there is no feasible path.
40
+
41
+
The time complexity is $O(m \times n),ドル and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns of the grid, respectively.
Copy file name to clipboardExpand all lines: lcci/08.04.Power Set/README_EN.md
+15-1Lines changed: 15 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,21 @@
40
40
41
41
## Solutions
42
42
43
-
Backtracking
43
+
**Solution 1: Recursive Enumeration**
44
+
45
+
We design a recursive function $dfs(u, t),ドル where $u$ is the index of the current element being enumerated, and $t$ is the current subset.
46
+
47
+
For the current element with index $u,ドル we can choose to add it to the subset $t,ドル or we can choose not to add it to the subset $t$. Recursively making these two choices will yield all subsets.
48
+
49
+
The time complexity is $O(n \times 2^n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the array. Each element in the array has two states, namely chosen or not chosen, for a total of 2ドル^n$ states. Each state requires $O(n)$ time to construct the subset.
50
+
51
+
**Solution 2: Binary Enumeration**
52
+
53
+
We can rewrite the recursive process in Method 1 into an iterative form, that is, using binary enumeration to enumerate all subsets.
54
+
55
+
We can use 2ドル^n$ binary numbers to represent all subsets of $n$ elements. If the $i$-th bit of a binary number `mask` is 1ドル,ドル it means that the subset contains the $i$-th element $v$ of the array; if it is 0ドル,ドル it means that the subset does not contain the $i$-th element $v$ of the array.
56
+
57
+
The time complexity is $O(n \times 2^n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the array. There are a total of 2ドル^n$ subsets, and each subset requires $O(n)$ time to construct.
0 commit comments