-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode32_longest_valid_parentheses.cpp
98 lines (86 loc) · 2.91 KB
/
leetcode32_longest_valid_parentheses.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @file leetcode32_longest_valid_parentheses.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2022/07/31 17:44:31
* @brief https://leetcode.com/problems/longest-valid-parentheses/
*
**/
#include <iostream>
#include <vector>
#include <string>
class Solution {
public:
int longestValidParentheses(std::string s) {
/*
* Key points: Dynamic Programming
*
* First come up in mind: for each i and j, check if s[i:j] contains
* valid parentheses. But there are many duplicate sub problems.
*
* 1) Starting from each pair of closed '()' patterns
* 2) If we have known s[i:j] contains valid parentheses, then when we
* go to s[i - 1], there may be several possible cases:
* (1) s[i - 1] == '(', s[i:j] has valid parentheses, s[j+1] = ')'
* (2) s[j-1 : j] has valid parentheses, s[j + 1: k] has valid
* parenetheses
* in either case the length starting from s[i - 1] should be updated
*
* Corner case:
* in case (1), if we find s[i - 1: j + 1] has valid parentheses, we want
* to further check if there are consequtive substrings that also has
* valid prentheses
*
* example: "((()))())"
*/
ssize_t len = s.length();
std::vector<int> result_vec(len, 0);
for (ssize_t i = len - 2; i >= 0; --i) {
if (s[i] == '(' && s[i+1] == ')') {
result_vec[i] = 2;
}
}
for (ssize_t i = len - 2; i >= 0; --i) {
int len1 = 0;
if (s[i] == '('
&& result_vec[i+1] > 0
&& (i + result_vec[i+1] + 1) < len
&& s[i + result_vec[i+1] + 1] == ')') {
len1 = result_vec[i+1] + 2;
}
if ((i + len1 < len) && result_vec[i + len1] > 0) {
len1 += result_vec[i + len1];
}
int len2 = 0;
if (result_vec[i] > 0
&& (i + result_vec[i]) < len
&& result_vec[i + result_vec[i]] > 0) {
len2 = result_vec[i] + result_vec[i+result_vec[i]];
}
int max_len = std::max(len1, len2);
if (result_vec[i] < max_len) {
result_vec[i] = max_len;
}
}
int max_len = 0;
for (ssize_t i = 0; i < len; ++i) {
if (max_len < result_vec[i]) {
max_len = result_vec[i];
}
}
return max_len;
}
};
int main() {
while (1) {
std::cout << "Input string (only '(' and ')'), empty string to exit: ";
std::string s;
std::getline(std::cin, s);
if (s.empty()) {
return 0;
}
Solution solution;
int ret = solution.longestValidParentheses(s);
std::cout << ret << std::endl;
}
return 0;
}