86
86
87
87
<!-- 这里可写通用的实现逻辑 -->
88
88
89
+ ** 方法一:贪心**
90
+
91
+ 我们用一个变量 $x$ 维护当前括号的平衡度,也就是左括号的数量减去右括号的数量。
92
+
93
+ 遍历字符串 $seq$,更新 $x$ 的值。如果 $x$ 为偶数,我们将当前的左括号分给 $A$,否则分给 $B$。
94
+
95
+ 时间复杂度 $O(n)$,其中 $n$ 是字符串 $seq$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
96
+
89
97
<!-- tabs:start -->
90
98
91
99
### ** Python3**
96
104
class Solution :
97
105
def maxDepthAfterSplit (self , seq : str ) -> List[int ]:
98
106
ans = [0 ] * len (seq)
99
- a = b = 0
107
+ x = 0
100
108
for i, c in enumerate (seq):
101
109
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
107
112
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
113
115
return ans
114
116
```
115
117
116
118
### ** Java**
117
119
118
120
<!-- 这里可写当前语言的特殊实现逻辑 -->
119
121
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
-
136
122
``` java
137
123
class Solution {
138
124
public int [] maxDepthAfterSplit (String seq ) {
139
125
int n = seq. length();
140
126
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 ;
151
130
} else {
152
- if (a > b) {
153
- -- a;
154
- } else {
155
- -- b;
156
- ans[i] = 1 ;
157
- }
131
+ ans[i] = -- x & 1 ;
158
132
}
159
133
}
160
134
return ans;
@@ -170,19 +144,11 @@ public:
170
144
vector<int > maxDepthAfterSplit(string seq) {
171
145
int n = seq.size();
172
146
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;
181
150
} else {
182
- if (a > b)
183
- --a;
184
- else
185
- --b, ans[ i] = 1;
151
+ ans[ i] = --x & 1;
186
152
}
187
153
}
188
154
return ans;
@@ -194,29 +160,38 @@ public:
194
160
195
161
```go
196
162
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++
207
169
} else {
208
- if a > b {
209
- a--
210
- } else {
211
- b--
212
- ans[i] = 1
213
- }
170
+ x--
171
+ ans[i] = x & 1
214
172
}
215
173
}
216
174
return ans
217
175
}
218
176
```
219
177
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
+
220
195
### ** ...**
221
196
222
197
```
0 commit comments