-
Notifications
You must be signed in to change notification settings - Fork 0
/
Valid.cpp
48 lines (47 loc) · 978 Bytes
/
Valid.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
class Valid {
public:
// use a stack
int longestValidParentheses(string s) {
stack<int> stack;
int start = 0, longest = 0;
for (int end = 0; end < s.size(); end++) {
if (s[end] == '(') {
stack.push(end);
} else {
// s[end] == ')'
if (stack.empty()) {
start = end + 1;
} else {
stack.pop();
longest = stack.empty() ? max(longest, end - start + 1) : max(longest, end - stack.top());
}
}
}
return longest;
}
// dp
int longestValidParenthesesDP(string s) {
if (size() == 0) {
return 0;
}
vector<int> dp(s.size() + 1, 0);
int count = 0, longest = 0;
for (int i = 1; i <= s.size(); i++) {
if (s[i - 1] == '(') {
count++;
} else {
// s[i - 1] == ')'
count--;
dp[i] = 2;
if (s[i - 1] == ')') {
// add former closed ')'
dp[i] += dp[i - 1];
}
// add previous valid substring
dp[i] += dp[i - dp[i]];
longest = max(longest, dp[i]);
}
}
return longest;
}
};