()()(((....

We can count the first 2 in only when match the ( after that,

()()( .. )

So just need stack the count when a left parenthesis appear.

```
public int longestValidParentheses(String s) {
Deque<Integer> stack = new ArrayDeque<Integer>();
int longest = 0;
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push(count);
count = 0;
} else if (stack.size() > 0) {
count += stack.poll() + 1;
longest = Math.max(longest, count);
} else {
count = 0;
stack.clear();
}
}
return longest * 2;
}
```