-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathLongestValidParentheses.cs
executable file
·47 lines (44 loc) · 2.79 KB
/
LongestValidParentheses.cs
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
// Source : https://leetcode.com/problems/longest-valid-parentheses/
// Author : codeyu
// Date : Saturday, October 15, 2016 11:24:32 AM
/**********************************************************************************
*
* Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
*
*
* For "(()", the longest valid parentheses substring is "()", which has length = 2.
*
*
* Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution032 {
public static int LongestValidParentheses(string s)
{
if(s.Length == 0) return 0;
var result = 0;
var dp = new List<int>{ 0 };
for(var i=1; i<=s.Length; i++)
{
dp.Add(0);
int j = i - 2 - dp[i-1];
if(s[i-1] == '(' || j < 0 || s[j] == ')')
{
dp[i] = 0;
}
else
{
dp[i] = dp[i-1]+2+dp[j];
result = Math.Max(result, dp[i]);
}
}
return result;
}
}
}