Skip to content

Commit b2a6b78

Browse files
authored
feat: add solutions to lc problems: No.0328~0330 (doocs#2265)
1 parent 2c82292 commit b2a6b78

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

solution/0300-0399/0328.Odd Even Linked List/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@
3737

3838
## Solutions
3939

40-
### Solution 1
40+
### Solution 1: Single Pass
41+
42+
We can use two pointers $a$ and $b$ to represent the tail nodes of the odd and even nodes respectively. Initially, pointer $a$ points to the head node $head$ of the list, and pointer $b$ points to the second node $head.next$ of the list. In addition, we use a pointer $c$ to point to the head node $head.next$ of the even nodes, which is the initial position of pointer $b$.
43+
44+
We traverse the list, set pointer $a$ to point to the next node of $b$, i.e., $a.next = b.next$, then move pointer $a$ back by one position, i.e., $a = a.next$; set pointer $b$ to point to the next node of $a$, i.e., $b.next = a.next$, then move pointer $b$ back by one position, i.e., $b = b.next$. Continue to traverse until $b$ reaches the end of the list.
45+
46+
Finally, we set the tail node $a$ of the odd nodes to point to the head node $c$ of the even nodes, i.e., $a.next = c$, then return the head node $head$ of the list.
47+
48+
The time complexity is $O(n)$, where $n$ is the length of the list, and we need to traverse the list once. The space complexity is $O(1)$. We only need to maintain a limited number of pointers.
4149

4250
<!-- tabs:start -->
4351

solution/0300-0399/0329.Longest Increasing Path in a Matrix/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,20 @@
4444

4545
## Solutions
4646

47-
### Solution 1
47+
### Solution 1: Memoization Search
48+
49+
We design a function $dfs(i, j)$, which represents the length of the longest increasing path that can be obtained starting from the coordinate $(i, j)$ in the matrix. The answer is $\max_{i, j} \textit{dfs}(i, j)$.
50+
51+
The execution logic of the function $dfs(i, j)$ is as follows:
52+
53+
- If $(i, j)$ has been visited, directly return $\textit{f}(i, j)$;
54+
- Otherwise, search $(i, j)$, search the coordinates $(x, y)$ in four directions. If $0 \le x < m, 0 \le y < n$ and $matrix[x][y] > matrix[i][j]$, then search $(x, y)$. After the search is over, update $\textit{f}(i, j)$ to $\textit{f}(i, j) = \max(\textit{f}(i, j), \textit{f}(x, y) + 1)$. Finally, return $\textit{f}(i, j)$.
55+
56+
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.
57+
58+
Similar problems:
59+
60+
- [2328. Number of Increasing Paths in a Grid](https://github.com/doocs/leetcode/blob/main/solution/2300-2399/2328.Number%20of%20Increasing%20Paths%20in%20a%20Grid/README_EN.md)
4861

4962
<!-- tabs:start -->
5063

solution/0300-0399/0330.Patching Array/README_EN.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,26 @@ Explanation: The two patches can be [2, 4].
4848

4949
## Solutions
5050

51-
### Solution 1
51+
### Solution 1: Greedy
52+
53+
Let's assume that the number $x$ is the smallest positive integer that cannot be represented. Then all the numbers in $[1,..x-1]$ can be represented. In order to represent the number $x$, we need to add a number that is less than or equal to $x$:
54+
55+
- If the added number equals $x$, since all numbers in $[1,..x-1]$ can be represented, after adding $x$, all numbers in the range $[1,..2x-1]$ can be represented, and the smallest positive integer that cannot be represented becomes $2x$.
56+
- If the added number is less than $x$, let's assume it's $x'$, since all numbers in $[1,..x-1]$ can be represented, after adding $x'$, all numbers in the range $[1,..x+x'-1]$ can be represented, and the smallest positive integer that cannot be represented becomes $x+x' \lt 2x$.
57+
58+
Therefore, we should greedily add the number $x$ to cover a larger range.
59+
60+
We use a variable $x$ to record the current smallest positive integer that cannot be represented, initialized to $1$. At this time, $[1,..x-1]$ is empty, indicating that no number can be covered; we use a variable $i$ to record the current index of the array being traversed.
61+
62+
We perform the following operations in a loop:
63+
64+
- If $i$ is within the range of the array and $nums[i] \le x$, it means that the current number can be covered, so we add the value of $nums[i]$ to $x$, and increment $i$ by $1$.
65+
- Otherwise, it means that $x$ is not covered, so we need to supplement a number $x$ in the array, and then update $x$ to $2x$.
66+
- Repeat the above operations until the value of $x$ is greater than $n$.
67+
68+
The final answer is the number of supplemented numbers.
69+
70+
The time complexity is $O(m + \log n)$, where $m$ is the length of the array $nums$. The space complexity is $O(1)$.
5271

5372
<!-- tabs:start -->
5473

solution/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def load_template(template_name: str) -> str:
3030
category_readme_en = load_template("category_readme_template_en")
3131

3232
category_dict = {
33-
'Database': '数据库',
33+
"Database": "数据库",
3434
}
3535

3636

0 commit comments

Comments
 (0)