Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b2a6b78

Browse files
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‎

Lines changed: 9 additions & 1 deletion
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‎

Lines changed: 14 additions & 1 deletion
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‎

Lines changed: 20 additions & 1 deletion
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 2ドルx$.
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 2ドルx$.
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‎

Lines changed: 1 addition & 1 deletion
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)

AltStyle によって変換されたページ (->オリジナル) /