Skip to content

Commit cf06c76

Browse files
committed
feat: add solutions to lc problem: No.1541
No.1541.Minimum Insertions to Balance a Parentheses String
1 parent ff74483 commit cf06c76

File tree

7 files changed

+302
-30
lines changed

7 files changed

+302
-30
lines changed

solution/1000-1099/1096.Brace Expansion II/Solution.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ def dfs(exp):
88
i = j
99
while exp[i] != '{':
1010
i -= 1
11-
a, c, = exp[:i], exp[j + 1:]
12-
for b in exp[i + 1: j].split(','):
11+
a, c, = (
12+
exp[:i],
13+
exp[j + 1 :],
14+
)
15+
for b in exp[i + 1 : j].split(','):
1316
dfs(a + b + c)
1417

1518
s = set()

solution/1500-1599/1541.Minimum Insertions to Balance a Parentheses String/README.md

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,140 @@
6969

7070
<!-- 这里可写通用的实现逻辑 -->
7171

72+
**方法一:贪心**
73+
74+
我们用 $x$ 表示字符串中待匹配的左括号的数量,初始时为 $0$。遍历字符串 $s$:
75+
76+
如果遇到左括号,则 $x$ 的值加 $1$;如果遇到右括号,我们分情况讨论:
77+
78+
- 如果有两个连续的右括号,那么我们先让指针往后移动一位;否则,我们需要插入一个右括号,使得出现两个连续的右括号,因此插入次数加 $1$;
79+
- 如果 $x = 0$,说明当前没有待匹配的左括号,我们需要插入一个左括号,用于匹配上面准备好的两个连续的右括号,因此插入次数加 $1$;否则,我们让 $x$ 的值减 $1$。
80+
81+
然后指针往后移动一位,继续下一次遍历。
82+
83+
遍历结束后,如果 $x = 0$,说明字符串已经平衡,返回插入次数;否则,说明字符串中有待匹配的左括号,我们需要再插入 $2 \times x$ 个右括号,使得字符串变成平衡字符串,返回插入次数。
84+
85+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
86+
7287
<!-- tabs:start -->
7388

7489
### **Python3**
7590

7691
<!-- 这里可写当前语言的特殊实现逻辑 -->
7792

7893
```python
79-
94+
class Solution:
95+
def minInsertions(self, s: str) -> int:
96+
ans = x = 0
97+
i, n = 0, len(s)
98+
while i < n:
99+
if s[i] == '(':
100+
# 待匹配的左括号加 1
101+
x += 1
102+
else:
103+
if i < n - 1 and s[i + 1] == ')':
104+
# 有连续两个右括号,i 往后移动
105+
i += 1
106+
else:
107+
# 只有一个右括号,插入一个
108+
ans += 1
109+
if x == 0:
110+
# 无待匹配的左括号,插入一个
111+
ans += 1
112+
else:
113+
# 待匹配的左括号减 1
114+
x -= 1
115+
i += 1
116+
# 遍历结束,仍有待匹配的左括号,说明右括号不足,插入 x << 1 个
117+
ans += x << 1
118+
return ans
80119
```
81120

82121
### **Java**
83122

84123
<!-- 这里可写当前语言的特殊实现逻辑 -->
85124

86125
```java
126+
class Solution {
127+
public int minInsertions(String s) {
128+
int ans = 0, x = 0;
129+
int n = s.length();
130+
for (int i = 0; i < n; ++i) {
131+
if (s.charAt(i) == '(') {
132+
++x;
133+
} else {
134+
if (i < n - 1 && s.charAt(i + 1) == ')') {
135+
++i;
136+
} else {
137+
++ans;
138+
}
139+
if (x == 0) {
140+
++ans;
141+
} else {
142+
--x;
143+
}
144+
}
145+
}
146+
ans += x << 1;
147+
return ans;
148+
}
149+
}
150+
```
151+
152+
### **C++**
153+
154+
```cpp
155+
class Solution {
156+
public:
157+
int minInsertions(string s) {
158+
int ans = 0, x = 0;
159+
int n = s.size();
160+
for (int i = 0; i < n; ++i) {
161+
if (s[i] == '(') {
162+
++x;
163+
} else {
164+
if (i < n - 1 && s[i + 1] == ')') {
165+
++i;
166+
} else {
167+
++ans;
168+
}
169+
if (x == 0) {
170+
++ans;
171+
} else {
172+
--x;
173+
}
174+
}
175+
}
176+
ans += x << 1;
177+
return ans;
178+
}
179+
};
180+
```
87181
182+
### **Go**
183+
184+
```go
185+
func minInsertions(s string) int {
186+
ans, x, n := 0, 0, len(s)
187+
for i := 0; i < n; i++ {
188+
if s[i] == '(' {
189+
x++
190+
} else {
191+
if i < n-1 && s[i+1] == ')' {
192+
i++
193+
} else {
194+
ans++
195+
}
196+
if x == 0 {
197+
ans++
198+
} else {
199+
x--
200+
}
201+
}
202+
}
203+
ans += x << 1
204+
return ans
205+
}
88206
```
89207

90208
### **...**

solution/1500-1599/1541.Minimum Insertions to Balance a Parentheses String/README_EN.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,110 @@
6161
### **Python3**
6262

6363
```python
64-
64+
class Solution:
65+
def minInsertions(self, s: str) -> int:
66+
ans = x = 0
67+
i, n = 0, len(s)
68+
while i < n:
69+
if s[i] == '(':
70+
x += 1
71+
else:
72+
if i < n - 1 and s[i + 1] == ')':
73+
i += 1
74+
else:
75+
ans += 1
76+
if x == 0:
77+
ans += 1
78+
else:
79+
x -= 1
80+
i += 1
81+
ans += x << 1
82+
return ans
6583
```
6684

6785
### **Java**
6886

6987
```java
88+
class Solution {
89+
public int minInsertions(String s) {
90+
int ans = 0, x = 0;
91+
int n = s.length();
92+
for (int i = 0; i < n; ++i) {
93+
if (s.charAt(i) == '(') {
94+
++x;
95+
} else {
96+
if (i < n - 1 && s.charAt(i + 1) == ')') {
97+
++i;
98+
} else {
99+
++ans;
100+
}
101+
if (x == 0) {
102+
++ans;
103+
} else {
104+
--x;
105+
}
106+
}
107+
}
108+
ans += x << 1;
109+
return ans;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int minInsertions(string s) {
120+
int ans = 0, x = 0;
121+
int n = s.size();
122+
for (int i = 0; i < n; ++i) {
123+
if (s[i] == '(') {
124+
++x;
125+
} else {
126+
if (i < n - 1 && s[i + 1] == ')') {
127+
++i;
128+
} else {
129+
++ans;
130+
}
131+
if (x == 0) {
132+
++ans;
133+
} else {
134+
--x;
135+
}
136+
}
137+
}
138+
ans += x << 1;
139+
return ans;
140+
}
141+
};
142+
```
70143
144+
### **Go**
145+
146+
```go
147+
func minInsertions(s string) int {
148+
ans, x, n := 0, 0, len(s)
149+
for i := 0; i < n; i++ {
150+
if s[i] == '(' {
151+
x++
152+
} else {
153+
if i < n-1 && s[i+1] == ')' {
154+
i++
155+
} else {
156+
ans++
157+
}
158+
if x == 0 {
159+
ans++
160+
} else {
161+
x--
162+
}
163+
}
164+
}
165+
ans += x << 1
166+
return ans
167+
}
71168
```
72169

73170
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int minInsertions(string s) {
4+
int ans = 0, x = 0;
5+
int n = s.size();
6+
for (int i = 0; i < n; ++i) {
7+
if (s[i] == '(') {
8+
++x;
9+
} else {
10+
if (i < n - 1 && s[i + 1] == ')') {
11+
++i;
12+
} else {
13+
++ans;
14+
}
15+
if (x == 0) {
16+
++ans;
17+
} else {
18+
--x;
19+
}
20+
}
21+
}
22+
ans += x << 1;
23+
return ans;
24+
}
25+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func minInsertions(s string) int {
2+
ans, x, n := 0, 0, len(s)
3+
for i := 0; i < n; i++ {
4+
if s[i] == '(' {
5+
x++
6+
} else {
7+
if i < n-1 && s[i+1] == ')' {
8+
i++
9+
} else {
10+
ans++
11+
}
12+
if x == 0 {
13+
ans++
14+
} else {
15+
x--
16+
}
17+
}
18+
}
19+
ans += x << 1
20+
return ans
21+
}
Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
11
class Solution {
22
public int minInsertions(String s) {
3-
int left = 0;
4-
int res = 0;
5-
char[] chars = s.toCharArray();
6-
for (int i = 0; i < chars.length;) {
7-
if (chars[i] == '(') {
8-
left++;
9-
i++;
3+
int ans = 0, x = 0;
4+
int n = s.length();
5+
for (int i = 0; i < n; ++i) {
6+
if (s.charAt(i) == '(') {
7+
++x;
108
} else {
11-
// 连续2个 )
12-
if (i < chars.length - 1 && chars[i + 1] == ')') {
13-
if (left > 0) {
14-
left--;
15-
} else {
16-
res++;
17-
}
18-
i += 2;
9+
if (i < n - 1 && s.charAt(i + 1) == ')') {
10+
++i;
1911
} else {
20-
if (left > 0) {
21-
left--;
22-
res++;
23-
} else {
24-
res += 2;
25-
}
26-
i++;
12+
++ans;
13+
}
14+
if (x == 0) {
15+
++ans;
16+
} else {
17+
--x;
2718
}
2819
}
2920
}
30-
if (left > 0) {
31-
res += 2 * left;
32-
}
33-
return res;
21+
ans += x << 1;
22+
return ans;
3423
}
3524
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def minInsertions(self, s: str) -> int:
3+
ans = x = 0
4+
i, n = 0, len(s)
5+
while i < n:
6+
if s[i] == '(':
7+
x += 1
8+
else:
9+
if i < n - 1 and s[i + 1] == ')':
10+
i += 1
11+
else:
12+
ans += 1
13+
if x == 0:
14+
ans += 1
15+
else:
16+
x -= 1
17+
i += 1
18+
ans += x << 1
19+
return ans

0 commit comments

Comments
 (0)