forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0032.cpp
33 lines (32 loc) · 750 Bytes
/
0032.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int x = [](){std::ios::sync_with_stdio(false);cin.tie(0);return 0;}();
class Solution
{
public:
int longestValidParentheses(string s)
{
int result = 0;
vector<int> mem(s.size(), 0);
for (int i = 1; i < s.size(); ++i)
{
if (s[i] == ')')
{
int left = i - 1 - mem[i-1];
if (left >= 0 and s[left] == '(')
{
mem[i] = mem[i-1] + 2;
}
if (i >= mem[i]) mem[i] += mem[i-mem[i]];
}
result = max(result, mem[i]);
}
return result;
}
};
int main()
{
return 0;
}