Maximum Nesting Depth of Two Valid Parentheses Strings LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> maxDepthAfterSplit(string seq) {
4
// since we want the difference to be as low as possible so we will try to
5
// balance both A and B by trying to maintain the number of paranthesis as
6
// close as close as possible
7
vector<int> indexA, indexB, res(seq.length(), 0);
8
// initailly assuming all parenthesis belong to A so filling res with 0
9
int i = 0;
10
int addToA = 0, addToB = 0;
11
while (i < seq.length()) {
12
if (seq[i] == '(') {
13
if (addToA <= addToB) {
14
// adding depth to A when it's depth is lesser or equal to b
15
indexA.push_back(i);
16
addToA++;
17
} else {
18
indexB.push_back(i);
19
addToB++;
20
}
21
} else {
22
// removing depth from string whose depth is maximum as we have to keep
23
// the difference minimum
24
if (addToA >= addToB) {
25
addToA--;
26
indexA.push_back(i);
27
} else {
28
indexB.push_back(i);
29
addToB--;
30
}
31
}
32
i++;
33
}
34
for (i = 0; i < indexB.size(); i++) {
35
res[indexB[i]] = 1;
36
}
37
return res;
38
}
39
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0