Skip to content

Commit b0b7eb9

Browse files
authored
Create maximum-nesting-depth-of-two-valid-parentheses-strings.py
1 parent 2b9710e commit b0b7eb9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def maxDepthAfterSplit(self, seq):
6+
"""
7+
:type seq: str
8+
:rtype: List[int]
9+
"""
10+
return [(i & 1) ^ (seq[i] == '(') for i, c in enumerate(seq)]
11+
12+
13+
# Time: O(n)
14+
# Space: O(1)
15+
class Solution2(object):
16+
def maxDepthAfterSplit(self, seq):
17+
"""
18+
:type seq: str
19+
:rtype: List[int]
20+
"""
21+
A, B = 0, 0
22+
result = [0] * len(seq)
23+
for i, c in enumerate(seq):
24+
point = 1 if c == '(' else -1
25+
if (point == 1 and A <= B) or \
26+
(point == -1 and A >= B):
27+
A += point
28+
else:
29+
B += point
30+
result[i] = 1
31+
return result

0 commit comments

Comments
 (0)