Longest Valid Parenthese 问题的 DP 解法

Catalogue

来源:Leetcode: 32. Longest Valid Parentheses

Longest Valid Parentheses
Given a string containing just the characters ‘(‘ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.

要求子序列的所有括号是连续的、无间断的。

Example 1:

1
2
3
Input: "(()(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Example 2:

1
2
3
Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"

我的思路,使用动态规划,可以找到其最优子结构。具体如下图所示:

JavaScript 的实现:

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
/**
* @param {string} s
* @return {number}
*/
var longestValidParenthesesDP = function(s) {
let max = 0;
let dp = new Array(s.length).fill(0);

for (var i = 1; i < s.length; i++) {
if (s[i] === '(') {
dp[i] = 0;
}

if ((s[i] === ')') && (s[i - 1] === '(')) {
let left = i - 2 >= 0 ? dp[i - 2] : 0;
dp[i] = left + 2;
}

if ((s[i] === ')') && (s[i - 1] === ')')) {
let x = i - dp[i - 1] - 1;
if (x >= 0 && s[x] === '(') {
let left = x - 1 >= 0 ? dp[x - 1] : 0;
dp[i] = dp[i - 1] + left + 2;
} else {
dp[i] = 0;
}
}

max = Math.max(max, dp[i]);
}

return max;
};

// 测试用例
let tests = [{
s: "()"
}, {
s: "(()",
}, {
s: ")()())"
}, {
s: "(()())"
}, {
s: "(()()))))())"
}];

tests.forEach(item => {
var res = longestValidParenthesesDP(item.s);
console.log(res);
});
Share