From bda019b2802dac389237135b1ed7c76b0af7662b Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sat, 27 Jan 2024 12:12:59 +0000 Subject: [PATCH 1/2] feat: add solutions to lc problems: No.0328~0330 --- .../0328.Odd Even Linked List/README_EN.md | 10 ++++++++- .../README_EN.md | 15 ++++++++++++- .../0330.Patching Array/README_EN.md | 21 ++++++++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/solution/0300-0399/0328.Odd Even Linked List/README_EN.md b/solution/0300-0399/0328.Odd Even Linked List/README_EN.md index e60ccc0afa1fc..a02c7894ed183 100644 --- a/solution/0300-0399/0328.Odd Even Linked List/README_EN.md +++ b/solution/0300-0399/0328.Odd Even Linked List/README_EN.md @@ -37,7 +37,15 @@ ## Solutions -### Solution 1 +### Solution 1: Single Pass + +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$. + +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. + +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. + +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. diff --git a/solution/0300-0399/0329.Longest Increasing Path in a Matrix/README_EN.md b/solution/0300-0399/0329.Longest Increasing Path in a Matrix/README_EN.md index 7fc1a47565938..13b48fc690aaf 100644 --- a/solution/0300-0399/0329.Longest Increasing Path in a Matrix/README_EN.md +++ b/solution/0300-0399/0329.Longest Increasing Path in a Matrix/README_EN.md @@ -44,7 +44,20 @@ ## Solutions -### Solution 1 +### Solution 1: Memoization Search + +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)$. + +The execution logic of the function $dfs(i, j)$ is as follows: + +- If $(i, j)$ has been visited, directly return $\textit{f}(i, j)$; +- 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)$. + +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. + +Similar problems: + +- [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) diff --git a/solution/0300-0399/0330.Patching Array/README_EN.md b/solution/0300-0399/0330.Patching Array/README_EN.md index b7df64b975d82..af41f351611ce 100644 --- a/solution/0300-0399/0330.Patching Array/README_EN.md +++ b/solution/0300-0399/0330.Patching Array/README_EN.md @@ -48,7 +48,26 @@ Explanation: The two patches can be [2, 4]. ## Solutions -### Solution 1 +### Solution 1: Greedy + +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$: + +- 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$. +- 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$. + +Therefore, we should greedily add the number $x$ to cover a larger range. + +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. + +We perform the following operations in a loop: + +- 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$. +- 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$. +- Repeat the above operations until the value of $x$ is greater than $n$. + +The final answer is the number of supplemented numbers. + +The time complexity is $O(m + \log n)$, where $m$ is the length of the array $nums$. The space complexity is $O(1)$. From a37bd3be615c08c419d5cd6f18475b02038c20be Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sat, 27 Jan 2024 12:15:45 +0000 Subject: [PATCH 2/2] style: format code --- solution/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/util.py b/solution/util.py index ec46b8260542e..ef04812ccec88 100644 --- a/solution/util.py +++ b/solution/util.py @@ -30,7 +30,7 @@ def load_template(template_name: str) -> str: category_readme_en = load_template("category_readme_template_en") category_dict = { - 'Database': '数据库', + "Database": "数据库", }