-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0032_longest_valid_parentheses.rs
More file actions
47 lines (42 loc) · 1.47 KB
/
s0032_longest_valid_parentheses.rs
File metadata and controls
47 lines (42 loc) · 1.47 KB
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
#![allow(unused)]
pub struct Solution {}
use std::cmp::max;
impl Solution {
// O(n) O(n)
pub fn longest_valid_parentheses(s: String) -> i32 {
if s.len() < 2 {
return 0;
}
let (mut ans, mut dp, chs) = (0, vec![0; s.len()], s.chars().collect::<Vec<char>>());
for i in 1..chs.len() {
if chs[i] == ')' {
if chs[i - 1] == '(' {
dp[i] = (if i >= 2 { dp[i - 2] } else { 0 }) + 2;
} else if i as i32 - dp[i - 1] > 0 && chs[i - dp[i - 1] as usize - 1] == '(' {
dp[i] = dp[i - 1]
+ (if i as i32 - dp[i - 1] >= 2 {
dp[i - dp[i - 1] as usize - 2]
} else {
0
})
+ 2;
}
ans = max(ans, dp[i]);
}
}
ans
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_20() {
assert_eq!(Solution::longest_valid_parentheses("(()".to_owned()), 2);
assert_eq!(Solution::longest_valid_parentheses("()(()".to_owned()), 2);
assert_eq!(Solution::longest_valid_parentheses("()((())".to_owned()), 4);
assert_eq!(Solution::longest_valid_parentheses("()(())".to_owned()), 6);
assert_eq!(Solution::longest_valid_parentheses(")()())".to_owned()), 4);
assert_eq!(Solution::longest_valid_parentheses("".to_owned()), 0);
}
}