Skip to content

Commit 54a8eef

Browse files
committed
feat: add solutions to lc problem: No.1111
No.1111.Maximum Nesting Depth of Two Valid Parentheses Strings
1 parent ab26852 commit 54a8eef

File tree

6 files changed

+292
-3
lines changed

6 files changed

+292
-3
lines changed

solution/0700-0799/0761.Special Binary String/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def makeLargestSpecial(self, s: str) -> str:
88
while i < len(s):
99
cnt += 1 if s[i] == '1' else -1
1010
if cnt == 0:
11-
ans.append('1' + self.makeLargestSpecial(s[j + 1: i]) + '0')
11+
ans.append('1' + self.makeLargestSpecial(s[j + 1 : i]) + '0')
1212
j = i + 1
1313
i += 1
1414
ans.sort(reverse=True)

solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/README.md

+114-1
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,128 @@
9393
<!-- 这里可写当前语言的特殊实现逻辑 -->
9494

9595
```python
96-
96+
class Solution:
97+
def maxDepthAfterSplit(self, seq: str) -> List[int]:
98+
ans = [0] * len(seq)
99+
a = b = 0
100+
for i, c in enumerate(seq):
101+
if c == "(":
102+
if a < b:
103+
a += 1
104+
else:
105+
b += 1
106+
ans[i] = 1
107+
else:
108+
if a > b:
109+
a -= 1
110+
else:
111+
b -= 1
112+
ans[i] = 1
113+
return ans
97114
```
98115

99116
### **Java**
100117

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

103120
```java
121+
class Solution {
122+
public int[] maxDepthAfterSplit(String seq) {
123+
int[] res = new int[seq.length()];
124+
for (int i = 0, cnt = 0; i < res.length; ++i) {
125+
if (seq.charAt(i) == '(') {
126+
res[i] = cnt++ & 1;
127+
} else {
128+
res[i] = --cnt & 1;
129+
}
130+
}
131+
return res;
132+
}
133+
}
134+
```
135+
136+
```java
137+
class Solution {
138+
public int[] maxDepthAfterSplit(String seq) {
139+
int n = seq.length();
140+
int[] ans = new int[n];
141+
int a = 0, b = 0;
142+
for (int i = 0; i < n; ++i) {
143+
char c = seq.charAt(i);
144+
if (c == '(') {
145+
if (a < b) {
146+
++a;
147+
} else {
148+
++b;
149+
ans[i] = 1;
150+
}
151+
} else {
152+
if (a > b) {
153+
--a;
154+
} else {
155+
--b;
156+
ans[i] = 1;
157+
}
158+
}
159+
}
160+
return ans;
161+
}
162+
}
163+
```
164+
165+
### **C++**
166+
167+
```cpp
168+
class Solution {
169+
public:
170+
vector<int> maxDepthAfterSplit(string seq) {
171+
int n = seq.size();
172+
vector<int> ans(n);
173+
int a = 0, b = 0;
174+
for (int i = 0; i < n; ++i)
175+
{
176+
char c = seq[i];
177+
if (c == '(')
178+
{
179+
if (a < b) ++a;
180+
else ++b, ans[i] = 1;
181+
}
182+
else
183+
{
184+
if (a > b) --a;
185+
else --b, ans[i] = 1;
186+
}
187+
}
188+
return ans;
189+
}
190+
};
191+
```
104192
193+
### **Go**
194+
195+
```go
196+
func maxDepthAfterSplit(seq string) []int {
197+
ans := make([]int, len(seq))
198+
a, b := 0, 0
199+
for i, c := range seq {
200+
if c == '(' {
201+
if a < b {
202+
a++
203+
} else {
204+
b++
205+
ans[i] = 1
206+
}
207+
} else {
208+
if a > b {
209+
a--
210+
} else {
211+
b--
212+
ans[i] = 1
213+
}
214+
}
215+
}
216+
return ans
217+
}
105218
```
106219

107220
### **...**

solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/README_EN.md

+114-1
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,126 @@
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def maxDepthAfterSplit(self, seq: str) -> List[int]:
64+
ans = [0] * len(seq)
65+
a = b = 0
66+
for i, c in enumerate(seq):
67+
if c == "(":
68+
if a < b:
69+
a += 1
70+
else:
71+
b += 1
72+
ans[i] = 1
73+
else:
74+
if a > b:
75+
a -= 1
76+
else:
77+
b -= 1
78+
ans[i] = 1
79+
return ans
6380
```
6481

6582
### **Java**
6683

6784
```java
85+
class Solution {
86+
public int[] maxDepthAfterSplit(String seq) {
87+
int[] res = new int[seq.length()];
88+
for (int i = 0, cnt = 0; i < res.length; ++i) {
89+
if (seq.charAt(i) == '(') {
90+
res[i] = cnt++ & 1;
91+
} else {
92+
res[i] = --cnt & 1;
93+
}
94+
}
95+
return res;
96+
}
97+
}
98+
```
99+
100+
```java
101+
class Solution {
102+
public int[] maxDepthAfterSplit(String seq) {
103+
int n = seq.length();
104+
int[] ans = new int[n];
105+
int a = 0, b = 0;
106+
for (int i = 0; i < n; ++i) {
107+
char c = seq.charAt(i);
108+
if (c == '(') {
109+
if (a < b) {
110+
++a;
111+
} else {
112+
++b;
113+
ans[i] = 1;
114+
}
115+
} else {
116+
if (a > b) {
117+
--a;
118+
} else {
119+
--b;
120+
ans[i] = 1;
121+
}
122+
}
123+
}
124+
return ans;
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
vector<int> maxDepthAfterSplit(string seq) {
135+
int n = seq.size();
136+
vector<int> ans(n);
137+
int a = 0, b = 0;
138+
for (int i = 0; i < n; ++i)
139+
{
140+
char c = seq[i];
141+
if (c == '(')
142+
{
143+
if (a < b) ++a;
144+
else ++b, ans[i] = 1;
145+
}
146+
else
147+
{
148+
if (a > b) --a;
149+
else --b, ans[i] = 1;
150+
}
151+
}
152+
return ans;
153+
}
154+
};
155+
```
68156
157+
### **Go**
158+
159+
```go
160+
func maxDepthAfterSplit(seq string) []int {
161+
ans := make([]int, len(seq))
162+
a, b := 0, 0
163+
for i, c := range seq {
164+
if c == '(' {
165+
if a < b {
166+
a++
167+
} else {
168+
b++
169+
ans[i] = 1
170+
}
171+
} else {
172+
if a > b {
173+
a--
174+
} else {
175+
b--
176+
ans[i] = 1
177+
}
178+
}
179+
}
180+
return ans
181+
}
69182
```
70183

71184
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<int> maxDepthAfterSplit(string seq) {
4+
int n = seq.size();
5+
vector<int> ans(n);
6+
int a = 0, b = 0;
7+
for (int i = 0; i < n; ++i)
8+
{
9+
char c = seq[i];
10+
if (c == '(')
11+
{
12+
if (a < b) ++a;
13+
else ++b, ans[i] = 1;
14+
}
15+
else
16+
{
17+
if (a > b) --a;
18+
else --b, ans[i] = 1;
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def maxDepthAfterSplit(self, seq: str) -> List[int]:
3+
ans = [0] * len(seq)
4+
a = b = 0
5+
for i, c in enumerate(seq):
6+
if c == "(":
7+
if a < b:
8+
a += 1
9+
else:
10+
b += 1
11+
ans[i] = 1
12+
else:
13+
if a > b:
14+
a -= 1
15+
else:
16+
b -= 1
17+
ans[i] = 1
18+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func maxDepthAfterSplit(seq string) []int {
2+
ans := make([]int, len(seq))
3+
a, b := 0, 0
4+
for i, c := range seq {
5+
if c == '(' {
6+
if a < b {
7+
a++
8+
} else {
9+
b++
10+
ans[i] = 1
11+
}
12+
} else {
13+
if a > b {
14+
a--
15+
} else {
16+
b--
17+
ans[i] = 1
18+
}
19+
}
20+
}
21+
return ans
22+
}

0 commit comments

Comments
 (0)