Skip to content

Commit bc7eb86

Browse files
authored
feat: add solutions to lc problems: No.0921~0923 (#2433)
1 parent 8ecec84 commit bc7eb86

File tree

14 files changed

+207
-37
lines changed

14 files changed

+207
-37
lines changed

solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ func minAddToMakeValid(s string) int {
124124
}
125125
```
126126

127+
```ts
128+
function minAddToMakeValid(s: string): number {
129+
const stk: string[] = [];
130+
for (const c of s) {
131+
if (c === ')' && stk.length > 0 && stk.at(-1)! === '(') {
132+
stk.pop();
133+
} else {
134+
stk.push(c);
135+
}
136+
}
137+
return stk.length;
138+
}
139+
```
140+
127141
<!-- tabs:end -->
128142

129143
### 方法二:贪心 + 计数
@@ -139,7 +153,7 @@ func minAddToMakeValid(s string) int {
139153

140154
遍历结束后,将 `cnt` 的值加到 `ans` 中,即为答案。
141155

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

144158
<!-- tabs:start -->
145159

@@ -213,6 +227,23 @@ func minAddToMakeValid(s string) int {
213227
}
214228
```
215229

230+
```ts
231+
function minAddToMakeValid(s: string): number {
232+
let [ans, cnt] = [0, 0];
233+
for (const c of s) {
234+
if (c === '(') {
235+
++cnt;
236+
} else if (cnt) {
237+
--cnt;
238+
} else {
239+
++ans;
240+
}
241+
}
242+
ans += cnt;
243+
return ans;
244+
}
245+
```
246+
216247
<!-- tabs:end -->
217248

218249
<!-- end -->

solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README_EN.md

+57-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,18 @@
4747

4848
## Solutions
4949

50-
### Solution 1
50+
### Solution 1: Greedy + Stack
51+
52+
This problem is a classic parenthesis matching problem, which can be solved using "Greedy + Stack".
53+
54+
Iterate through each character $c$ in the string $s$:
55+
56+
- If $c$ is a left parenthesis, directly push $c$ into the stack;
57+
- 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.
58+
59+
After the iteration ends, the number of remaining elements in the stack is the number of parentheses that need to be added.
60+
61+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.
5162

5263
<!-- tabs:start -->
5364

@@ -109,9 +120,36 @@ func minAddToMakeValid(s string) int {
109120
}
110121
```
111122

123+
```ts
124+
function minAddToMakeValid(s: string): number {
125+
const stk: string[] = [];
126+
for (const c of s) {
127+
if (c === ')' && stk.length > 0 && stk.at(-1)! === '(') {
128+
stk.pop();
129+
} else {
130+
stk.push(c);
131+
}
132+
}
133+
return stk.length;
134+
}
135+
```
136+
112137
<!-- tabs:end -->
113138

114-
### Solution 2
139+
### Solution 2: Greedy + Counting
140+
141+
Solution 1 uses a stack to implement parenthesis matching, but we can also directly implement it through counting.
142+
143+
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$.
144+
145+
Iterate through each character $c$ in the string $s$:
146+
147+
- If $c$ is a left parenthesis, increase the value of `cnt` by $1$;
148+
- 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$.
149+
150+
After the iteration ends, add the value of `cnt` to `ans`, which is the answer.
151+
152+
The time complexity is $O(n)$, and the space complexity is $O(1)$, where $n$ is the length of the string $s$.
115153

116154
<!-- tabs:start -->
117155

@@ -185,6 +223,23 @@ func minAddToMakeValid(s string) int {
185223
}
186224
```
187225

226+
```ts
227+
function minAddToMakeValid(s: string): number {
228+
let [ans, cnt] = [0, 0];
229+
for (const c of s) {
230+
if (c === '(') {
231+
++cnt;
232+
} else if (cnt) {
233+
--cnt;
234+
} else {
235+
++ans;
236+
}
237+
}
238+
ans += cnt;
239+
return ans;
240+
}
241+
```
242+
188243
<!-- tabs:end -->
189244

190245
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function minAddToMakeValid(s: string): number {
2+
const stk: string[] = [];
3+
for (const c of s) {
4+
if (c === ')' && stk.length > 0 && stk.at(-1)! === '(') {
5+
stk.pop();
6+
} else {
7+
stk.push(c);
8+
}
9+
}
10+
return stk.length;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function minAddToMakeValid(s: string): number {
2+
let [ans, cnt] = [0, 0];
3+
for (const c of s) {
4+
if (c === '(') {
5+
++cnt;
6+
} else if (cnt) {
7+
--cnt;
8+
} else {
9+
++ans;
10+
}
11+
}
12+
ans += cnt;
13+
return ans;
14+
}

solution/0900-0999/0922.Sort Array By Parity II/README.md

+31-11
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@
4848

4949
## 解法
5050

51-
### 方法一
51+
### 方法一:双指针
52+
53+
我们用两个指针 $i$ 和 $j$ 分别指向偶数下标和奇数下标。
54+
55+
当 $i$ 指向偶数下标时,如果 $nums[i]$ 是奇数,那么我们需要找到一个奇数下标 $j$,使得 $nums[j]$ 是偶数,然后交换 $nums[i]$ 和 $nums[j]$。继续遍历,直到 $i$ 指向数组末尾。
56+
57+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
5258

5359
<!-- tabs:start -->
5460

@@ -57,8 +63,8 @@ class Solution:
5763
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
5864
n, j = len(nums), 1
5965
for i in range(0, n, 2):
60-
if (nums[i] & 1) == 1:
61-
while (nums[j] & 1) == 1:
66+
if nums[i] % 2:
67+
while nums[j] % 2:
6268
j += 2
6369
nums[i], nums[j] = nums[j], nums[i]
6470
return nums
@@ -68,8 +74,8 @@ class Solution:
6874
class Solution {
6975
public int[] sortArrayByParityII(int[] nums) {
7076
for (int i = 0, j = 1; i < nums.length; i += 2) {
71-
if ((nums[i] & 1) == 1) {
72-
while ((nums[j] & 1) == 1) {
77+
if (nums[i] % 2 == 1) {
78+
while (nums[j] % 2 == 1) {
7379
j += 2;
7480
}
7581
int t = nums[i];
@@ -87,8 +93,8 @@ class Solution {
8793
public:
8894
vector<int> sortArrayByParityII(vector<int>& nums) {
8995
for (int i = 0, j = 1; i < nums.size(); i += 2) {
90-
if ((nums[i] & 1) == 1) {
91-
while ((nums[j] & 1) == 1) {
96+
if (nums[i] % 2) {
97+
while (nums[j] % 2) {
9298
j += 2;
9399
}
94100
swap(nums[i], nums[j]);
@@ -102,8 +108,8 @@ public:
102108
```go
103109
func sortArrayByParityII(nums []int) []int {
104110
for i, j := 0, 1; i < len(nums); i += 2 {
105-
if (nums[i] & 1) == 1 {
106-
for (nums[j] & 1) == 1 {
111+
if nums[i]%2 == 1 {
112+
for nums[j]%2 == 1 {
107113
j += 2
108114
}
109115
nums[i], nums[j] = nums[j], nums[i]
@@ -113,15 +119,29 @@ func sortArrayByParityII(nums []int) []int {
113119
}
114120
```
115121

122+
```ts
123+
function sortArrayByParityII(nums: number[]): number[] {
124+
for (let i = 0, j = 1; i < nums.length; i += 2) {
125+
if (nums[i] % 2) {
126+
while (nums[j] % 2) {
127+
j += 2;
128+
}
129+
[nums[i], nums[j]] = [nums[j], nums[i]];
130+
}
131+
}
132+
return nums;
133+
}
134+
```
135+
116136
```js
117137
/**
118138
* @param {number[]} nums
119139
* @return {number[]}
120140
*/
121141
var sortArrayByParityII = function (nums) {
122142
for (let i = 0, j = 1; i < nums.length; i += 2) {
123-
if ((nums[i] & 1) == 1) {
124-
while ((nums[j] & 1) == 1) {
143+
if (nums[i] % 2) {
144+
while (nums[j] % 2) {
125145
j += 2;
126146
}
127147
[nums[i], nums[j]] = [nums[j], nums[i]];

solution/0900-0999/0922.Sort Array By Parity II/README_EN.md

+31-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@
4343

4444
## Solutions
4545

46-
### Solution 1
46+
### Solution 1: Two Pointers
47+
48+
We use two pointers $i$ and $j$ to point to even and odd indices respectively.
49+
50+
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.
51+
52+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
4753

4854
<!-- tabs:start -->
4955

@@ -52,8 +58,8 @@ class Solution:
5258
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
5359
n, j = len(nums), 1
5460
for i in range(0, n, 2):
55-
if (nums[i] & 1) == 1:
56-
while (nums[j] & 1) == 1:
61+
if nums[i] % 2:
62+
while nums[j] % 2:
5763
j += 2
5864
nums[i], nums[j] = nums[j], nums[i]
5965
return nums
@@ -63,8 +69,8 @@ class Solution:
6369
class Solution {
6470
public int[] sortArrayByParityII(int[] nums) {
6571
for (int i = 0, j = 1; i < nums.length; i += 2) {
66-
if ((nums[i] & 1) == 1) {
67-
while ((nums[j] & 1) == 1) {
72+
if (nums[i] % 2 == 1) {
73+
while (nums[j] % 2 == 1) {
6874
j += 2;
6975
}
7076
int t = nums[i];
@@ -82,8 +88,8 @@ class Solution {
8288
public:
8389
vector<int> sortArrayByParityII(vector<int>& nums) {
8490
for (int i = 0, j = 1; i < nums.size(); i += 2) {
85-
if ((nums[i] & 1) == 1) {
86-
while ((nums[j] & 1) == 1) {
91+
if (nums[i] % 2) {
92+
while (nums[j] % 2) {
8793
j += 2;
8894
}
8995
swap(nums[i], nums[j]);
@@ -97,8 +103,8 @@ public:
97103
```go
98104
func sortArrayByParityII(nums []int) []int {
99105
for i, j := 0, 1; i < len(nums); i += 2 {
100-
if (nums[i] & 1) == 1 {
101-
for (nums[j] & 1) == 1 {
106+
if nums[i]%2 == 1 {
107+
for nums[j]%2 == 1 {
102108
j += 2
103109
}
104110
nums[i], nums[j] = nums[j], nums[i]
@@ -108,15 +114,29 @@ func sortArrayByParityII(nums []int) []int {
108114
}
109115
```
110116

117+
```ts
118+
function sortArrayByParityII(nums: number[]): number[] {
119+
for (let i = 0, j = 1; i < nums.length; i += 2) {
120+
if (nums[i] % 2) {
121+
while (nums[j] % 2) {
122+
j += 2;
123+
}
124+
[nums[i], nums[j]] = [nums[j], nums[i]];
125+
}
126+
}
127+
return nums;
128+
}
129+
```
130+
111131
```js
112132
/**
113133
* @param {number[]} nums
114134
* @return {number[]}
115135
*/
116136
var sortArrayByParityII = function (nums) {
117137
for (let i = 0, j = 1; i < nums.length; i += 2) {
118-
if ((nums[i] & 1) == 1) {
119-
while ((nums[j] & 1) == 1) {
138+
if (nums[i] % 2) {
139+
while (nums[j] % 2) {
120140
j += 2;
121141
}
122142
[nums[i], nums[j]] = [nums[j], nums[i]];

solution/0900-0999/0922.Sort Array By Parity II/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class Solution {
22
public:
33
vector<int> sortArrayByParityII(vector<int>& nums) {
44
for (int i = 0, j = 1; i < nums.size(); i += 2) {
5-
if ((nums[i] & 1) == 1) {
6-
while ((nums[j] & 1) == 1) {
5+
if (nums[i] % 2) {
6+
while (nums[j] % 2) {
77
j += 2;
88
}
99
swap(nums[i], nums[j]);

solution/0900-0999/0922.Sort Array By Parity II/Solution.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
func sortArrayByParityII(nums []int) []int {
22
for i, j := 0, 1; i < len(nums); i += 2 {
3-
if (nums[i] & 1) == 1 {
4-
for (nums[j] & 1) == 1 {
3+
if nums[i]%2 == 1 {
4+
for nums[j]%2 == 1 {
55
j += 2
66
}
77
nums[i], nums[j] = nums[j], nums[i]

solution/0900-0999/0922.Sort Array By Parity II/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution {
22
public int[] sortArrayByParityII(int[] nums) {
33
for (int i = 0, j = 1; i < nums.length; i += 2) {
4-
if ((nums[i] & 1) == 1) {
5-
while ((nums[j] & 1) == 1) {
4+
if (nums[i] % 2 == 1) {
5+
while (nums[j] % 2 == 1) {
66
j += 2;
77
}
88
int t = nums[i];

solution/0900-0999/0922.Sort Array By Parity II/Solution.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*/
55
var sortArrayByParityII = function (nums) {
66
for (let i = 0, j = 1; i < nums.length; i += 2) {
7-
if ((nums[i] & 1) == 1) {
8-
while ((nums[j] & 1) == 1) {
7+
if (nums[i] % 2) {
8+
while (nums[j] % 2) {
99
j += 2;
1010
}
1111
[nums[i], nums[j]] = [nums[j], nums[i]];

solution/0900-0999/0922.Sort Array By Parity II/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class Solution:
22
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
33
n, j = len(nums), 1
44
for i in range(0, n, 2):
5-
if (nums[i] & 1) == 1:
6-
while (nums[j] & 1) == 1:
5+
if nums[i] % 2:
6+
while nums[j] % 2:
77
j += 2
88
nums[i], nums[j] = nums[j], nums[i]
99
return nums

0 commit comments

Comments
 (0)