diff --git a/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README.md b/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README.md index c92db5e2a79da..70afa1c63e64b 100644 --- a/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README.md +++ b/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README.md @@ -124,6 +124,20 @@ func minAddToMakeValid(s string) int { } ``` +```ts +function minAddToMakeValid(s: string): number { + const stk: string[] = []; + for (const c of s) { + if (c === ')' && stk.length > 0 && stk.at(-1)! === '(') { + stk.pop(); + } else { + stk.push(c); + } + } + return stk.length; +} +``` + ### 方法二:贪心 + 计数 @@ -139,7 +153,7 @@ func minAddToMakeValid(s string) int { 遍历结束后,将 `cnt` 的值加到 `ans` 中,即为答案。 -时间复杂度为 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度为 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。 @@ -213,6 +227,23 @@ func minAddToMakeValid(s string) int { } ``` +```ts +function minAddToMakeValid(s: string): number { + let [ans, cnt] = [0, 0]; + for (const c of s) { + if (c === '(') { + ++cnt; + } else if (cnt) { + --cnt; + } else { + ++ans; + } + } + ans += cnt; + return ans; +} +``` + diff --git a/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README_EN.md b/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README_EN.md index bee222a2039f3..ed0bbae1f2eb9 100644 --- a/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README_EN.md +++ b/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README_EN.md @@ -47,7 +47,18 @@ ## Solutions -### Solution 1 +### Solution 1: Greedy + Stack + +This problem is a classic parenthesis matching problem, which can be solved using "Greedy + Stack". + +Iterate through each character $c$ in the string $s$: + +- If $c$ is a left parenthesis, directly push $c$ into the stack; +- If $c$ is a right parenthesis, at this point if the stack is not empty, and the top element of the stack is a left parenthesis, then pop the top element of the stack, indicating a successful match; otherwise, push $c$ into the stack. + +After the iteration ends, the number of remaining elements in the stack is the number of parentheses that need to be added. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$. @@ -109,9 +120,36 @@ func minAddToMakeValid(s string) int { } ``` +```ts +function minAddToMakeValid(s: string): number { + const stk: string[] = []; + for (const c of s) { + if (c === ')' && stk.length > 0 && stk.at(-1)! === '(') { + stk.pop(); + } else { + stk.push(c); + } + } + return stk.length; +} +``` + -### Solution 2 +### Solution 2: Greedy + Counting + +Solution 1 uses a stack to implement parenthesis matching, but we can also directly implement it through counting. + +Define a variable `cnt` to represent the current number of left parentheses to be matched, and a variable `ans` to record the answer. Initially, both variables are set to $0$. + +Iterate through each character $c$ in the string $s$: + +- If $c$ is a left parenthesis, increase the value of `cnt` by $1$; +- If $c$ is a right parenthesis, at this point if $cnt > 0$, it means that there are left parentheses that can be matched, so decrease the value of `cnt` by $1$; otherwise, it means that the current right parenthesis cannot be matched, so increase the value of `ans` by $1$. + +After the iteration ends, add the value of `cnt` to `ans`, which is the answer. + +The time complexity is $O(n)$, and the space complexity is $O(1)$, where $n$ is the length of the string $s$. @@ -185,6 +223,23 @@ func minAddToMakeValid(s string) int { } ``` +```ts +function minAddToMakeValid(s: string): number { + let [ans, cnt] = [0, 0]; + for (const c of s) { + if (c === '(') { + ++cnt; + } else if (cnt) { + --cnt; + } else { + ++ans; + } + } + ans += cnt; + return ans; +} +``` + diff --git a/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/Solution.ts b/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/Solution.ts new file mode 100644 index 0000000000000..bdda6424bd8f9 --- /dev/null +++ b/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/Solution.ts @@ -0,0 +1,11 @@ +function minAddToMakeValid(s: string): number { + const stk: string[] = []; + for (const c of s) { + if (c === ')' && stk.length > 0 && stk.at(-1)! === '(') { + stk.pop(); + } else { + stk.push(c); + } + } + return stk.length; +} diff --git a/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/Solution2.ts b/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/Solution2.ts new file mode 100644 index 0000000000000..68ae58f0af442 --- /dev/null +++ b/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/Solution2.ts @@ -0,0 +1,14 @@ +function minAddToMakeValid(s: string): number { + let [ans, cnt] = [0, 0]; + for (const c of s) { + if (c === '(') { + ++cnt; + } else if (cnt) { + --cnt; + } else { + ++ans; + } + } + ans += cnt; + return ans; +} diff --git a/solution/0900-0999/0922.Sort Array By Parity II/README.md b/solution/0900-0999/0922.Sort Array By Parity II/README.md index e01aea16b6ef2..920b4eb4fff64 100644 --- a/solution/0900-0999/0922.Sort Array By Parity II/README.md +++ b/solution/0900-0999/0922.Sort Array By Parity II/README.md @@ -48,7 +48,13 @@ ## 解法 -### 方法一 +### 方法一:双指针 + +我们用两个指针 $i$ 和 $j$ 分别指向偶数下标和奇数下标。 + +当 $i$ 指向偶数下标时,如果 $nums[i]$ 是奇数,那么我们需要找到一个奇数下标 $j$,使得 $nums[j]$ 是偶数,然后交换 $nums[i]$ 和 $nums[j]$。继续遍历,直到 $i$ 指向数组末尾。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。 @@ -57,8 +63,8 @@ class Solution: def sortArrayByParityII(self, nums: List[int]) -> List[int]: n, j = len(nums), 1 for i in range(0, n, 2): - if (nums[i] & 1) == 1: - while (nums[j] & 1) == 1: + if nums[i] % 2: + while nums[j] % 2: j += 2 nums[i], nums[j] = nums[j], nums[i] return nums @@ -68,8 +74,8 @@ class Solution: class Solution { public int[] sortArrayByParityII(int[] nums) { for (int i = 0, j = 1; i < nums.length; i += 2) { - if ((nums[i] & 1) == 1) { - while ((nums[j] & 1) == 1) { + if (nums[i] % 2 == 1) { + while (nums[j] % 2 == 1) { j += 2; } int t = nums[i]; @@ -87,8 +93,8 @@ class Solution { public: vector sortArrayByParityII(vector& nums) { for (int i = 0, j = 1; i < nums.size(); i += 2) { - if ((nums[i] & 1) == 1) { - while ((nums[j] & 1) == 1) { + if (nums[i] % 2) { + while (nums[j] % 2) { j += 2; } swap(nums[i], nums[j]); @@ -102,8 +108,8 @@ public: ```go func sortArrayByParityII(nums []int) []int { for i, j := 0, 1; i < len(nums); i += 2 { - if (nums[i] & 1) == 1 { - for (nums[j] & 1) == 1 { + if nums[i]%2 == 1 { + for nums[j]%2 == 1 { j += 2 } nums[i], nums[j] = nums[j], nums[i] @@ -113,6 +119,20 @@ func sortArrayByParityII(nums []int) []int { } ``` +```ts +function sortArrayByParityII(nums: number[]): number[] { + for (let i = 0, j = 1; i < nums.length; i += 2) { + if (nums[i] % 2) { + while (nums[j] % 2) { + j += 2; + } + [nums[i], nums[j]] = [nums[j], nums[i]]; + } + } + return nums; +} +``` + ```js /** * @param {number[]} nums @@ -120,8 +140,8 @@ func sortArrayByParityII(nums []int) []int { */ var sortArrayByParityII = function (nums) { for (let i = 0, j = 1; i < nums.length; i += 2) { - if ((nums[i] & 1) == 1) { - while ((nums[j] & 1) == 1) { + if (nums[i] % 2) { + while (nums[j] % 2) { j += 2; } [nums[i], nums[j]] = [nums[j], nums[i]]; diff --git a/solution/0900-0999/0922.Sort Array By Parity II/README_EN.md b/solution/0900-0999/0922.Sort Array By Parity II/README_EN.md index 26626d5f61365..2efeac636d4b4 100644 --- a/solution/0900-0999/0922.Sort Array By Parity II/README_EN.md +++ b/solution/0900-0999/0922.Sort Array By Parity II/README_EN.md @@ -43,7 +43,13 @@ ## Solutions -### Solution 1 +### Solution 1: Two Pointers + +We use two pointers $i$ and $j$ to point to even and odd indices respectively. + +When $i$ points to an even index, if $nums[i]$ is odd, then we need to find an odd index $j$ such that $nums[j]$ is even, and then swap $nums[i]$ and $nums[j]$. Continue to iterate until $i$ points to the end of the array. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -52,8 +58,8 @@ class Solution: def sortArrayByParityII(self, nums: List[int]) -> List[int]: n, j = len(nums), 1 for i in range(0, n, 2): - if (nums[i] & 1) == 1: - while (nums[j] & 1) == 1: + if nums[i] % 2: + while nums[j] % 2: j += 2 nums[i], nums[j] = nums[j], nums[i] return nums @@ -63,8 +69,8 @@ class Solution: class Solution { public int[] sortArrayByParityII(int[] nums) { for (int i = 0, j = 1; i < nums.length; i += 2) { - if ((nums[i] & 1) == 1) { - while ((nums[j] & 1) == 1) { + if (nums[i] % 2 == 1) { + while (nums[j] % 2 == 1) { j += 2; } int t = nums[i]; @@ -82,8 +88,8 @@ class Solution { public: vector sortArrayByParityII(vector& nums) { for (int i = 0, j = 1; i < nums.size(); i += 2) { - if ((nums[i] & 1) == 1) { - while ((nums[j] & 1) == 1) { + if (nums[i] % 2) { + while (nums[j] % 2) { j += 2; } swap(nums[i], nums[j]); @@ -97,8 +103,8 @@ public: ```go func sortArrayByParityII(nums []int) []int { for i, j := 0, 1; i < len(nums); i += 2 { - if (nums[i] & 1) == 1 { - for (nums[j] & 1) == 1 { + if nums[i]%2 == 1 { + for nums[j]%2 == 1 { j += 2 } nums[i], nums[j] = nums[j], nums[i] @@ -108,6 +114,20 @@ func sortArrayByParityII(nums []int) []int { } ``` +```ts +function sortArrayByParityII(nums: number[]): number[] { + for (let i = 0, j = 1; i < nums.length; i += 2) { + if (nums[i] % 2) { + while (nums[j] % 2) { + j += 2; + } + [nums[i], nums[j]] = [nums[j], nums[i]]; + } + } + return nums; +} +``` + ```js /** * @param {number[]} nums @@ -115,8 +135,8 @@ func sortArrayByParityII(nums []int) []int { */ var sortArrayByParityII = function (nums) { for (let i = 0, j = 1; i < nums.length; i += 2) { - if ((nums[i] & 1) == 1) { - while ((nums[j] & 1) == 1) { + if (nums[i] % 2) { + while (nums[j] % 2) { j += 2; } [nums[i], nums[j]] = [nums[j], nums[i]]; diff --git a/solution/0900-0999/0922.Sort Array By Parity II/Solution.cpp b/solution/0900-0999/0922.Sort Array By Parity II/Solution.cpp index bbb85caafee9d..ba623ad985fcb 100644 --- a/solution/0900-0999/0922.Sort Array By Parity II/Solution.cpp +++ b/solution/0900-0999/0922.Sort Array By Parity II/Solution.cpp @@ -2,8 +2,8 @@ class Solution { public: vector sortArrayByParityII(vector& nums) { for (int i = 0, j = 1; i < nums.size(); i += 2) { - if ((nums[i] & 1) == 1) { - while ((nums[j] & 1) == 1) { + if (nums[i] % 2) { + while (nums[j] % 2) { j += 2; } swap(nums[i], nums[j]); diff --git a/solution/0900-0999/0922.Sort Array By Parity II/Solution.go b/solution/0900-0999/0922.Sort Array By Parity II/Solution.go index 3a4e6e1563c01..c49f9e56c2a17 100644 --- a/solution/0900-0999/0922.Sort Array By Parity II/Solution.go +++ b/solution/0900-0999/0922.Sort Array By Parity II/Solution.go @@ -1,7 +1,7 @@ func sortArrayByParityII(nums []int) []int { for i, j := 0, 1; i < len(nums); i += 2 { - if (nums[i] & 1) == 1 { - for (nums[j] & 1) == 1 { + if nums[i]%2 == 1 { + for nums[j]%2 == 1 { j += 2 } nums[i], nums[j] = nums[j], nums[i] diff --git a/solution/0900-0999/0922.Sort Array By Parity II/Solution.java b/solution/0900-0999/0922.Sort Array By Parity II/Solution.java index 0dc516ac3a481..20d06eb6ef563 100644 --- a/solution/0900-0999/0922.Sort Array By Parity II/Solution.java +++ b/solution/0900-0999/0922.Sort Array By Parity II/Solution.java @@ -1,8 +1,8 @@ class Solution { public int[] sortArrayByParityII(int[] nums) { for (int i = 0, j = 1; i < nums.length; i += 2) { - if ((nums[i] & 1) == 1) { - while ((nums[j] & 1) == 1) { + if (nums[i] % 2 == 1) { + while (nums[j] % 2 == 1) { j += 2; } int t = nums[i]; diff --git a/solution/0900-0999/0922.Sort Array By Parity II/Solution.js b/solution/0900-0999/0922.Sort Array By Parity II/Solution.js index 54ca701be8ed9..aa97966324c18 100644 --- a/solution/0900-0999/0922.Sort Array By Parity II/Solution.js +++ b/solution/0900-0999/0922.Sort Array By Parity II/Solution.js @@ -4,8 +4,8 @@ */ var sortArrayByParityII = function (nums) { for (let i = 0, j = 1; i < nums.length; i += 2) { - if ((nums[i] & 1) == 1) { - while ((nums[j] & 1) == 1) { + if (nums[i] % 2) { + while (nums[j] % 2) { j += 2; } [nums[i], nums[j]] = [nums[j], nums[i]]; diff --git a/solution/0900-0999/0922.Sort Array By Parity II/Solution.py b/solution/0900-0999/0922.Sort Array By Parity II/Solution.py index ab2305811d013..b9ef473474b52 100644 --- a/solution/0900-0999/0922.Sort Array By Parity II/Solution.py +++ b/solution/0900-0999/0922.Sort Array By Parity II/Solution.py @@ -2,8 +2,8 @@ class Solution: def sortArrayByParityII(self, nums: List[int]) -> List[int]: n, j = len(nums), 1 for i in range(0, n, 2): - if (nums[i] & 1) == 1: - while (nums[j] & 1) == 1: + if nums[i] % 2: + while nums[j] % 2: j += 2 nums[i], nums[j] = nums[j], nums[i] return nums diff --git a/solution/0900-0999/0922.Sort Array By Parity II/Solution.ts b/solution/0900-0999/0922.Sort Array By Parity II/Solution.ts new file mode 100644 index 0000000000000..372e7a549a9f8 --- /dev/null +++ b/solution/0900-0999/0922.Sort Array By Parity II/Solution.ts @@ -0,0 +1,11 @@ +function sortArrayByParityII(nums: number[]): number[] { + for (let i = 0, j = 1; i < nums.length; i += 2) { + if (nums[i] % 2) { + while (nums[j] % 2) { + j += 2; + } + [nums[i], nums[j]] = [nums[j], nums[i]]; + } + } + return nums; +} diff --git a/solution/0900-0999/0923.3Sum With Multiplicity/README.md b/solution/0900-0999/0923.3Sum With Multiplicity/README.md index f7407668c93a1..8bb4e96b6d8cf 100644 --- a/solution/0900-0999/0923.3Sum With Multiplicity/README.md +++ b/solution/0900-0999/0923.3Sum With Multiplicity/README.md @@ -58,7 +58,7 @@ arr[i] = 1, arr[j] = arr[k] = 2 出现 12 次: 注意答案的取模操作。 -时间复杂度 $O(n^2)$,空间复杂度 $O(C)$。其中 $n$ 为数组 `arr` 的长度,而 $C$ 为数组 `cnt` 的长度。本题中 $C = 101$。 +时间复杂度 $O(n^2)$,空间复杂度 $O(C)$。其中 $n$ 为数组 `arr` 的长度;而 $C$ 为数组 `cnt` 的长度,本题中 $C = 101$。 diff --git a/solution/0900-0999/0923.3Sum With Multiplicity/README_EN.md b/solution/0900-0999/0923.3Sum With Multiplicity/README_EN.md index 6ec68a3e1cb39..c0f44dc563820 100644 --- a/solution/0900-0999/0923.3Sum With Multiplicity/README_EN.md +++ b/solution/0900-0999/0923.3Sum With Multiplicity/README_EN.md @@ -54,7 +54,15 @@ and two 2s from [2,2,2,2] in 6 ways. ## Solutions -### Solution 1 +### Solution 1: Enumeration + +First, we use an array `cnt` of length $101$ to record the number of occurrences of each element in the array `arr`. + +Then, we enumerate the elements in the array `arr` as the second element $b$ of the triplet, and first subtract one $b$ from `cnt`. Then, starting from the beginning of the array `arr`, we enumerate the first element $a$, and the third element $c$ is $target - a - b$. If the value of $c$ is within the range of the array `cnt`, then the answer is increased by $cnt[c]$. + +Note the modulo operation of the answer. + +The time complexity is $O(n^2)$, and the space complexity is $O(C)$. Where $n$ is the length of the array `arr`; and $C$ is the length of the array `cnt`, in this problem $C = 101$.