Skip to content

Commit 2f8ef10

Browse files
authored
feat: add solutions to lc problems: No.307+ (doocs#2143)
1 parent f65829d commit 2f8ef10

File tree

9 files changed

+78
-6
lines changed

9 files changed

+78
-6
lines changed

solution/0300-0399/0307.Range Sum Query - Mutable/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ numArray.sumRange(0, 2); // 返回 1 + 2 + 5 = 8
8484
- 线段树的每个叶子节点代表一个长度为 $1$ 的元区间 $[x, x]$;
8585
- 对于每个内部节点 $[l, r]$,它的左儿子是 $[l, mid]$,右儿子是 $[mid + 1, r]$, 其中 $mid = \lfloor \frac{l + r}{2} \rfloor$(即向下取整)。
8686

87+
对于本题,构造函数的时间复杂度为 $O(n \log n)$,其他操作的时间复杂度为 $O(\log n)$。空间复杂度为 $O(n)$。
88+
8789
<!-- tabs:start -->
8890

8991
### **Python3**

solution/0300-0399/0309.Best Time to Buy and Sell Stock with Cooldown/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $prices$ 的长度。
7373

74-
我们注意到,状态 $f[i][]$ 的转移只与 $f[i - 1][]$ 和 $f[i - 2][0]$ 有关,因此我们可以用三个变量 $f, f_0, f_1$ 代替数组 $f$,将空间复杂度优化到 $O(1)$。
74+
我们注意到,状态 $f[i][]$ 的转移只与 $f[i - 1][]$ 和 $f[i - 2][0]$ 有关,因此我们可以用三个变量 $f$, $f_0$, $f_1$ 代替数组 $f$,将空间复杂度优化到 $O(1)$。
7575

7676
<!-- tabs:start -->
7777

solution/0300-0399/0309.Best Time to Buy and Sell Stock with Cooldown/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@
4040

4141
## Solutions
4242

43+
**Solution 1: Memoization Search**
44+
45+
We design a function $dfs(i, j)$, which represents the maximum profit that can be obtained starting from the $i$th day with state $j$. The values of $j$ are $0$ and $1$, respectively representing currently not holding a stock and holding a stock. The answer is $dfs(0, 0)$.
46+
47+
The execution logic of the function $dfs(i, j)$ is as follows:
48+
49+
If $i \geq n$, it means that there are no more stocks to trade, so return $0$;
50+
51+
Otherwise, we can choose not to trade, then $dfs(i, j) = dfs(i + 1, j)$. We can also trade stocks. If $j > 0$, it means that we currently hold a stock and can sell it, then $dfs(i, j) = prices[i] + dfs(i + 2, 0)$. If $j = 0$, it means that we currently do not hold a stock and can buy, then $dfs(i, j) = -prices[i] + dfs(i + 1, 1)$. Take the maximum value as the return value of the function $dfs(i, j)$.
52+
53+
The answer is $dfs(0, 0)$.
54+
55+
To avoid repeated calculations, we use the method of memoization search, and use an array $f$ to record the return value of $dfs(i, j)$. If $f[i][j]$ is not $-1$, it means that it has been calculated, and we can directly return $f[i][j]$.
56+
57+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $prices$.
58+
59+
**Solution 2: Dynamic Programming**
60+
61+
We can also use dynamic programming to solve this problem.
62+
63+
We define $f[i][j]$ to represent the maximum profit that can be obtained on the $i$th day with state $j$. The values of $j$ are $0$ and $1$, respectively representing currently not holding a stock and holding a stock. Initially, $f[0][0] = 0$, $f[0][1] = -prices[0]$.
64+
65+
When $i \geq 1$, if we currently do not hold a stock, then $f[i][0]$ can be obtained by transitioning from $f[i - 1][0]$ and $f[i - 1][1] + prices[i]$, i.e., $f[i][0] = \max(f[i - 1][0], f[i - 1][1] + prices[i])$. If we currently hold a stock, then $f[i][1]$ can be obtained by transitioning from $f[i - 1][1]$ and $f[i - 2][0] - prices[i]$, i.e., $f[i][1] = \max(f[i - 1][1], f[i - 2][0] - prices[i])$. The final answer is $f[n - 1][0]$.
66+
67+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $prices$.
68+
69+
We notice that the transition of state $f[i][]$ is only related to $f[i - 1][]$ and $f[i - 2][0]$, so we can use three variables $f$, $f_0$, $f_1$ to replace the array $f$, optimizing the space complexity to $O(1)$.
70+
4371
<!-- tabs:start -->
4472

4573
### **Python3**

solution/0300-0399/0311.Sparse Matrix Multiplication/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@
4646

4747
我们可以直接按照矩阵乘法的定义,计算出结果矩阵中的每一个元素。
4848

49-
时间复杂度 $O(m \times n \times k)$,空间复杂度 $O(m \times n)$。
49+
时间复杂度 $O(m \times n \times k)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵 $mat1$ 的行数和矩阵 $mat2$ 的列数,而 $k$ 是矩阵 $mat1$ 的列数或矩阵 $mat2$ 的行数。
5050

5151
**方法二:预处理**
5252

5353
我们可以预处理出两个矩阵的稀疏表示,即 $g1[i]$ 表示矩阵 $mat1$ 第 $i$ 行中所有非零元素的列下标和值,而 $g2[i]$ 表示矩阵 $mat2$ 第 $i$ 行中所有非零元素的列下标和值。
5454

5555
接下来,我们遍历每一行 $i$,遍历 $g1[i]$ 中的每一个元素 $(k, x)$,遍历 $g2[k]$ 中的每一个元素 $(j, y)$,那么最终 $mat1[i][k] \times mat2[k][j]$ 就会对应到结果矩阵中的 $ans[i][j]$,我们将所有的结果累加即可。
5656

57-
时间复杂度 $O(m \times n \times k)$,空间复杂度 $O(m \times n)$。
57+
时间复杂度 $O(m \times n \times k)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵 $mat1$ 的行数和矩阵 $mat2$ 的列数,而 $k$ 是矩阵 $mat1$ 的列数或矩阵 $mat2$ 的行数。
5858

5959
<!-- tabs:start -->
6060

solution/0300-0399/0311.Sparse Matrix Multiplication/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@
3434

3535
## Solutions
3636

37+
**Solution 1: Direct Multiplication**
38+
39+
We can directly calculate each element in the result matrix according to the definition of matrix multiplication.
40+
41+
The time complexity is $O(m \times n \times k)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows of matrix $mat1$ and the number of columns of matrix $mat2$ respectively, and $k$ is the number of columns of matrix $mat1$ or the number of rows of matrix $mat2$.
42+
43+
**Solution 2: Preprocessing**
44+
45+
We can preprocess the sparse representation of the two matrices, i.e., $g1[i]$ represents the column index and value of all non-zero elements in the $i$th row of matrix $mat1$, and $g2[i]$ represents the column index and value of all non-zero elements in the $i$th row of matrix $mat2$.
46+
47+
Next, we traverse each row $i$, traverse each element $(k, x)$ in $g1[i]$, traverse each element $(j, y)$ in $g2[k]$, then $mat1[i][k] \times mat2[k][j]$ will correspond to $ans[i][j]$ in the result matrix, and we can accumulate all the results.
48+
49+
The time complexity is $O(m \times n \times k)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows of matrix $mat1$ and the number of columns of matrix $mat2$ respectively, and $k$ is the number of columns of matrix $mat1$ or the number of rows of matrix $mat2$.
50+
3751
<!-- tabs:start -->
3852

3953
### **Python3**

solution/0300-0399/0313.Super Ugly Number/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@
4040

4141
## Solutions
4242

43-
Priority Queue.
43+
**Solution 1: Priority Queue (Min Heap)**
44+
45+
We use a priority queue (min heap) to maintain all possible super ugly numbers, initially putting $1$ into the queue.
46+
47+
Each time we take the smallest super ugly number $x$ from the queue, multiply $x$ by each number in the array `primes`, and put the product into the queue. Repeat the above operation $n$ times to get the $n$th super ugly number.
48+
49+
Since the problem guarantees that the $n$th super ugly number is within the range of a 32-bit signed integer, before we put the product into the queue, we can first check whether the product exceeds $2^{31} - 1$. If it does, there is no need to put the product into the queue. In addition, the Euler sieve can be used for optimization.
50+
51+
The time complexity is $O(n \times m \times \log (n \times m))$, and the space complexity is $O(n \times m)$. Where $m$ and $n$ are the length of the array `primes` and the given integer $n$ respectively.
4452

4553
<!-- tabs:start -->
4654

solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@
4040

4141
## Solutions
4242

43+
**Solution 1: DFS**
44+
45+
DFS traverses the binary tree, recording the value, depth, and horizontal offset of each node. Then sort all nodes by horizontal offset from small to large, then by depth from small to large, and finally group by horizontal offset.
46+
47+
The time complexity is $O(n\log \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.
48+
49+
**Solution 2: BFS**
50+
51+
A better approach to this problem should be BFS, traversing from top to bottom level by level.
52+
53+
The time complexity is $O(n\log n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.
54+
4355
<!-- tabs:start -->
4456

4557
### **Python3**

solution/0300-0399/0316.Remove Duplicate Letters/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
最后将栈中元素拼接成字符串作为结果返回。
5050

51-
时间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
51+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
5252

5353
<!-- tabs:start -->
5454

solution/0300-0399/0316.Remove Duplicate Letters/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@
3434

3535
## Solutions
3636

37-
**Stack**
37+
**Solution 1: Stack**
38+
39+
We use an array `last` to record the last occurrence of each character, a stack to save the result string, and an array `vis` or an integer variable `mask` to record whether the current character is in the stack.
40+
41+
Traverse the string $s$, for each character $c$, if $c$ is not in the stack, we need to check whether the top element of the stack is greater than $c$. If it is greater than $c$ and the top element of the stack will appear later, we pop the top element of the stack and push $c$ into the stack.
42+
43+
Finally, concatenate the elements in the stack into a string and return it as the result.
44+
45+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
3846

3947
<!-- tabs:start -->
4048

0 commit comments

Comments
 (0)