-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path1111. Maximum Nesting Depth of Two Valid Parentheses Strings.cpp
81 lines (74 loc) · 2.65 KB
/
1111. Maximum Nesting Depth of Two Valid Parentheses Strings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/discuss/328841/JavaC%2B%2BPython-O(1)-Extra-Space-Except-Output
//sol 1
//Runtime: 20 ms, faster than 96.89% of C++ online submissions for Maximum Nesting Depth of Two Valid Parentheses Strings.
//Memory Usage: 9.4 MB, less than 100.00% of C++ online submissions for Maximum Nesting Depth of Two Valid Parentheses Strings.
class Solution {
public:
vector<int> maxDepthAfterSplit(string seq) {
int Adepth = 0, Bdepth = 0;
vector<int> ans(seq.size(), 0);
for(int i = 0; i < seq.size(); i++){
if(seq[i] == '('){
//here the condition is Adepth <= Bdepth
if(Adepth <= Bdepth){
Adepth++;
ans[i] = 0;
}else{
Bdepth++;
ans[i] = 1;
}
}else{
//here the condition is Adepth > Bdepth
//(the opposite of Adepth <= Bdepth)
if(Adepth > Bdepth){
Adepth--;
ans[i] = 0;
}else{
Bdepth--;
ans[i] = 1;
}
}
}
return ans;
}
};
//sol2
//Runtime: 20 ms, faster than 96.89% of C++ online submissions for Maximum Nesting Depth of Two Valid Parentheses Strings.
//Memory Usage: 9.5 MB, less than 100.00% of C++ online submissions for Maximum Nesting Depth of Two Valid Parentheses Strings.
class Solution {
public:
vector<int> maxDepthAfterSplit(string seq) {
int depth = 0, cur = 0;
vector<int> ans(seq.size());
for(char c : seq){
if(c == '('){
cur++;
depth = max(depth, cur);
}else{
cur--;
}
}
for(int i = 0; i < seq.size(); i++){
if(seq[i] == '(' && ++cur > depth/2) ans[i] = 1;
else if(seq[i] == ')' && cur-- > depth/2) ans[i] = 1;
}
return ans;
}
};
//sol 0, by parity
//Runtime: 20 ms, faster than 96.89% of C++ online submissions for Maximum Nesting Depth of Two Valid Parentheses Strings.
//Memory Usage: 9.5 MB, less than 100.00% of C++ online submissions for Maximum Nesting Depth of Two Valid Parentheses Strings.
class Solution {
public:
vector<int> maxDepthAfterSplit(string seq) {
vector<int> ans(seq.size(), 0);
for(int i = 0; i < seq.size(); i++){
if(seq[i] == '('){
ans[i] = i&1;
}else{
ans[i] = 1 - i&1;
}
}
return ans;
}
};