-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode00032.java
42 lines (38 loc) · 1.11 KB
/
Leetcode00032.java
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
import java.util.Stack;
/**
* https://leetcode.com/problems/longest-valid-parentheses
*
* @link 32. Longest Valid Parentheses
* @author vutiendat3601
* @date 2024-07-10
*/
public class Leetcode00032 {
public int longestValidParentheses(String s) {
final int n = s.length();
final int[] dp = new int[n];
final Stack<Integer> stack = new Stack<>();
final char[] chars = s.toCharArray();
int ans = 0;
for (int i = 0; i < n; i++) {
if (chars[i] == '(') {
stack.push(i);
} else if (chars[i] == ')') {
if (!stack.isEmpty()) {
final int idx = stack.pop();
dp[i] += dp[i - 1] + 1;
if (idx > 0) {
dp[i] += dp[idx - 1];
}
ans = Math.max(ans, dp[i]);
}
}
}
return ans * 2;
}
public static void main(String[] args) {
final String s = ")((()))()(()()()()()()";
final Leetcode00032 leetcode00032 = new Leetcode00032();
final int longestValidParentheses = leetcode00032.longestValidParentheses(s);
System.out.println("longestValidParentheses: " + longestValidParentheses);
}
}