Skip to content

Commit acd96e9

Browse files
committedMay 30, 2023
feat: add solutions to lc problem: No.1111
No.1111.Maximum Nesting Depth of Two Valid Parentheses Strings
1 parent cc97f12 commit acd96e9

File tree

7 files changed

+126
-171
lines changed

7 files changed

+126
-171
lines changed
 

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

+46-71
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@
8686

8787
<!-- 这里可写通用的实现逻辑 -->
8888

89+
**方法一:贪心**
90+
91+
我们用一个变量 $x$ 维护当前括号的平衡度,也就是左括号的数量减去右括号的数量。
92+
93+
遍历字符串 $seq$,更新 $x$ 的值。如果 $x$ 为偶数,我们将当前的左括号分给 $A$,否则分给 $B$。
94+
95+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $seq$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
96+
8997
<!-- tabs:start -->
9098

9199
### **Python3**
@@ -96,65 +104,31 @@
96104
class Solution:
97105
def maxDepthAfterSplit(self, seq: str) -> List[int]:
98106
ans = [0] * len(seq)
99-
a = b = 0
107+
x = 0
100108
for i, c in enumerate(seq):
101109
if c == "(":
102-
if a < b:
103-
a += 1
104-
else:
105-
b += 1
106-
ans[i] = 1
110+
ans[i] = x & 1
111+
x += 1
107112
else:
108-
if a > b:
109-
a -= 1
110-
else:
111-
b -= 1
112-
ans[i] = 1
113+
x -= 1
114+
ans[i] = x & 1
113115
return ans
114116
```
115117

116118
### **Java**
117119

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

120-
```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-
136122
```java
137123
class Solution {
138124
public int[] maxDepthAfterSplit(String seq) {
139125
int n = seq.length();
140126
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-
}
127+
for (int i = 0, x = 0; i < n; ++i) {
128+
if (seq.charAt(i) == '(') {
129+
ans[i] = x++ & 1;
151130
} else {
152-
if (a > b) {
153-
--a;
154-
} else {
155-
--b;
156-
ans[i] = 1;
157-
}
131+
ans[i] = --x & 1;
158132
}
159133
}
160134
return ans;
@@ -170,19 +144,11 @@ public:
170144
vector<int> maxDepthAfterSplit(string seq) {
171145
int n = seq.size();
172146
vector<int> ans(n);
173-
int a = 0, b = 0;
174-
for (int i = 0; i < n; ++i) {
175-
char c = seq[i];
176-
if (c == '(') {
177-
if (a < b)
178-
++a;
179-
else
180-
++b, ans[i] = 1;
147+
for (int i = 0, x = 0; i < n; ++i) {
148+
if (seq[i] == '(') {
149+
ans[i] = x++ & 1;
181150
} else {
182-
if (a > b)
183-
--a;
184-
else
185-
--b, ans[i] = 1;
151+
ans[i] = --x & 1;
186152
}
187153
}
188154
return ans;
@@ -194,29 +160,38 @@ public:
194160
195161
```go
196162
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-
}
163+
n := len(seq)
164+
ans := make([]int, n)
165+
for i, x := 0, 0; i < n; i++ {
166+
if seq[i] == '(' {
167+
ans[i] = x & 1
168+
x++
207169
} else {
208-
if a > b {
209-
a--
210-
} else {
211-
b--
212-
ans[i] = 1
213-
}
170+
x--
171+
ans[i] = x & 1
214172
}
215173
}
216174
return ans
217175
}
218176
```
219177

178+
### **TypeScript**
179+
180+
```ts
181+
function maxDepthAfterSplit(seq: string): number[] {
182+
const n = seq.length;
183+
const ans: number[] = new Array(n);
184+
for (let i = 0, x = 0; i < n; ++i) {
185+
if (seq[i] === '(') {
186+
ans[i] = x++ & 1;
187+
} else {
188+
ans[i] = --x & 1;
189+
}
190+
}
191+
return ans;
192+
}
193+
```
194+
220195
### **...**
221196

222197
```

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

+38-71
Original file line numberDiff line numberDiff line change
@@ -62,63 +62,29 @@
6262
class Solution:
6363
def maxDepthAfterSplit(self, seq: str) -> List[int]:
6464
ans = [0] * len(seq)
65-
a = b = 0
65+
x = 0
6666
for i, c in enumerate(seq):
6767
if c == "(":
68-
if a < b:
69-
a += 1
70-
else:
71-
b += 1
72-
ans[i] = 1
68+
ans[i] = x & 1
69+
x += 1
7370
else:
74-
if a > b:
75-
a -= 1
76-
else:
77-
b -= 1
78-
ans[i] = 1
71+
x -= 1
72+
ans[i] = x & 1
7973
return ans
8074
```
8175

8276
### **Java**
8377

84-
```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-
10078
```java
10179
class Solution {
10280
public int[] maxDepthAfterSplit(String seq) {
10381
int n = seq.length();
10482
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-
}
83+
for (int i = 0, x = 0; i < n; ++i) {
84+
if (seq.charAt(i) == '(') {
85+
ans[i] = x++ & 1;
11586
} else {
116-
if (a > b) {
117-
--a;
118-
} else {
119-
--b;
120-
ans[i] = 1;
121-
}
87+
ans[i] = --x & 1;
12288
}
12389
}
12490
return ans;
@@ -134,19 +100,11 @@ public:
134100
vector<int> maxDepthAfterSplit(string seq) {
135101
int n = seq.size();
136102
vector<int> ans(n);
137-
int a = 0, b = 0;
138-
for (int i = 0; i < n; ++i) {
139-
char c = seq[i];
140-
if (c == '(') {
141-
if (a < b)
142-
++a;
143-
else
144-
++b, ans[i] = 1;
103+
for (int i = 0, x = 0; i < n; ++i) {
104+
if (seq[i] == '(') {
105+
ans[i] = x++ & 1;
145106
} else {
146-
if (a > b)
147-
--a;
148-
else
149-
--b, ans[i] = 1;
107+
ans[i] = --x & 1;
150108
}
151109
}
152110
return ans;
@@ -158,29 +116,38 @@ public:
158116
159117
```go
160118
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-
}
119+
n := len(seq)
120+
ans := make([]int, n)
121+
for i, x := 0, 0; i < n; i++ {
122+
if seq[i] == '(' {
123+
ans[i] = x & 1
124+
x++
171125
} else {
172-
if a > b {
173-
a--
174-
} else {
175-
b--
176-
ans[i] = 1
177-
}
126+
x--
127+
ans[i] = x & 1
178128
}
179129
}
180130
return ans
181131
}
182132
```
183133

134+
### **TypeScript**
135+
136+
```ts
137+
function maxDepthAfterSplit(seq: string): number[] {
138+
const n = seq.length;
139+
const ans: number[] = new Array(n);
140+
for (let i = 0, x = 0; i < n; ++i) {
141+
if (seq[i] === '(') {
142+
ans[i] = x++ & 1;
143+
} else {
144+
ans[i] = --x & 1;
145+
}
146+
}
147+
return ans;
148+
}
149+
```
150+
184151
### **...**
185152

186153
```

‎solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/Solution.cpp

+4-12
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,11 @@ class Solution {
33
vector<int> maxDepthAfterSplit(string seq) {
44
int n = seq.size();
55
vector<int> ans(n);
6-
int a = 0, b = 0;
7-
for (int i = 0; i < n; ++i) {
8-
char c = seq[i];
9-
if (c == '(') {
10-
if (a < b)
11-
++a;
12-
else
13-
++b, ans[i] = 1;
6+
for (int i = 0, x = 0; i < n; ++i) {
7+
if (seq[i] == '(') {
8+
ans[i] = x++ & 1;
149
} else {
15-
if (a > b)
16-
--a;
17-
else
18-
--b, ans[i] = 1;
10+
ans[i] = --x & 1;
1911
}
2012
}
2113
return ans;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func maxDepthAfterSplit(seq string) []int {
2+
n := len(seq)
3+
ans := make([]int, n)
4+
for i, x := 0, 0; i < n; i++ {
5+
if seq[i] == '(' {
6+
ans[i] = x & 1
7+
x++
8+
} else {
9+
x--
10+
ans[i] = x & 1
11+
}
12+
}
13+
return ans
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
class Solution {
22
public int[] maxDepthAfterSplit(String seq) {
3-
int[] res = new int[seq.length()];
4-
for (int i = 0, cnt = 0; i < res.length; ++i) {
3+
int n = seq.length();
4+
int[] ans = new int[n];
5+
for (int i = 0, x = 0; i < n; ++i) {
56
if (seq.charAt(i) == '(') {
6-
res[i] = cnt++ & 1;
7+
ans[i] = x++ & 1;
78
} else {
8-
res[i] = --cnt & 1;
9+
ans[i] = --x & 1;
910
}
1011
}
11-
return res;
12+
return ans;
1213
}
13-
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
class Solution:
22
def maxDepthAfterSplit(self, seq: str) -> List[int]:
33
ans = [0] * len(seq)
4-
a = b = 0
4+
x = 0
55
for i, c in enumerate(seq):
66
if c == "(":
7-
if a < b:
8-
a += 1
9-
else:
10-
b += 1
11-
ans[i] = 1
7+
ans[i] = x & 1
8+
x += 1
129
else:
13-
if a > b:
14-
a -= 1
15-
else:
16-
b -= 1
17-
ans[i] = 1
10+
x -= 1
11+
ans[i] = x & 1
1812
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function maxDepthAfterSplit(seq: string): number[] {
2+
const n = seq.length;
3+
const ans: number[] = new Array(n);
4+
for (let i = 0, x = 0; i < n; ++i) {
5+
if (seq[i] === '(') {
6+
ans[i] = x++ & 1;
7+
} else {
8+
ans[i] = --x & 1;
9+
}
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)
Please sign in to comment.