-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path32.longest-valid-parentheses.kt
52 lines (51 loc) · 1.13 KB
/
32.longest-valid-parentheses.kt
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
/*
* @lc app=leetcode id=32 lang=kotlin
*
* [32] Longest Valid Parentheses
*
* https://leetcode.com/problems/longest-valid-parentheses/description/
*
* algorithms
* Hard (25.23%)
* Likes: 2094
* Dislikes: 96
* Total Accepted: 205.3K
* Total Submissions: 789.7K
* Testcase Example: '"(()"'
*
* Given a string containing just the characters '(' and ')', find the length
* of the longest valid (well-formed) parentheses substring.
*
* Example 1:
*
*
* Input: "(()"
* Output: 2
* Explanation: The longest valid parentheses substring is "()"
*
*
* Example 2:
*
*
* Input: ")()())"
* Output: 4
* Explanation: The longest valid parentheses substring is "()()"
*
*
*/
class Solution {
fun longestValidParentheses(s: String): Int {
var dp = IntArray(100000, {0})
var res = 0
for (i in 2 .. s.length) {
if (s[i-1] == ')') {
if (i-dp[i-1]-2 >= 0 && s[i-dp[i-1]-2] == '(') {
dp[i] = dp[i-1]+2
dp[i] += dp[i-dp[i]]
}
}
if (res < dp[i]) res = dp[i]
}
return res
}
}