You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README_EN.md
+57-2
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,18 @@
47
47
48
48
## Solutions
49
49
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$.
51
62
52
63
<!-- tabs:start -->
53
64
@@ -109,9 +120,36 @@ func minAddToMakeValid(s string) int {
109
120
}
110
121
```
111
122
123
+
```ts
124
+
function minAddToMakeValid(s:string):number {
125
+
const stk:string[] = [];
126
+
for (const c ofs) {
127
+
if (c===')'&&stk.length>0&&stk.at(-1)!==='(') {
128
+
stk.pop();
129
+
} else {
130
+
stk.push(c);
131
+
}
132
+
}
133
+
returnstk.length;
134
+
}
135
+
```
136
+
112
137
<!-- tabs:end -->
113
138
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$.
115
153
116
154
<!-- tabs:start -->
117
155
@@ -185,6 +223,23 @@ func minAddToMakeValid(s string) int {
Copy file name to clipboardExpand all lines: solution/0900-0999/0922.Sort Array By Parity II/README_EN.md
+31-11
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,13 @@
43
43
44
44
## Solutions
45
45
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)$.
0 commit comments