forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
48 lines (46 loc) · 1.21 KB
/
Solution.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
47
48
namespace LeetCodeNet.G0001_0100.S0032_longest_valid_parentheses {
// #Hard #Top_100_Liked_Questions #String #Dynamic_Programming #Stack #Big_O_Time_O(n)_Space_O(1)
// #2023_12_28_Time_49_ms_(96.18%)_Space_38.5_MB_(13.54%)
public class Solution {
public int LongestValidParentheses(string s) {
int max = 0;
int left = 0;
int right = 0;
int n = s.Length;
char ch;
for (int i = 0; i < n; i++) {
ch = s[i];
if (ch == '(') {
left++;
} else {
right++;
}
if (right > left) {
left = 0;
right = 0;
}
if (left == right) {
max = Math.Max(max, 2 * right);
}
}
left = 0;
right = 0;
for (int i = n - 1; i >= 0; i--) {
ch = s[i];
if (ch == '(') {
left++;
} else {
right++;
}
if (left > right) {
left = 0;
right = 0;
}
if (left == right) {
max = Math.Max(max, 2 * left);
}
}
return max;
}
}
}