<p>Given a string containing just the characters <code>&#39;(&#39;</code> and <code>&#39;)&#39;</code>, return <em>the length of the longest valid (well-formed) parentheses </em><span data-keyword="substring-nonempty"><em>substring</em></span>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(()&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> The longest valid parentheses substring is &quot;()&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;)()())&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest valid parentheses substring is &quot;()()&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;&quot;
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= s.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>s[i]</code> is <code>&#39;(&#39;</code>, or <code>&#39;)&#39;</code>.</li>
</ul>

class Solution {
public:
    int longestValidParentheses(string s) {
        int maxValid = 0;
        stack<int> stk; // ( -> 0
        for (const auto c : s) {
            if (c == '(') {
                stk.push(0);
            } else if (c == ')') {
                // case 0:   )
                if (stk.empty()) {
                    continue;
                }
                // case 1:  ?()
                else if (stk.top() == 0) {
                    stk.pop();
                    // case 1.0:   () -> 2
                    // case 1.1:  (() -> (2
                    if (stk.empty() || stk.top() == 0) {
                        stk.push(2);
                    } 
                    // case 1.2:  8() -> 10
                    else {
                        int newVal = stk.top() + 2;
                        stk.pop();
                        stk.push(newVal);
                    }
                }
                // case 2:  ?3)
                else {
                    int topVal = stk.top();
                    stk.pop();
                    // case 2.0:   3)
                    if (stk.empty()) {
                        maxValid = max(maxValid, topVal);
                    } 
                    // case 2.1:  ?(3)
                    else {
                        stk.pop();   
                        // case 2.1.2:  8(3) -> 13
                        if (!stk.empty() && stk.top() != 0) {
                            int newVal = stk.top();
                            stk.pop();
                            stk.push(newVal + topVal + 2);
                        } 
                        // case 2.1.1:  ((3) -> (5
                        else {
                            stk.push(topVal + 2);
                        }
                    }
                }
            }
        }
        while (!stk.empty()) {
            maxValid = max(maxValid, stk.top());
            stk.pop();
        }
        return maxValid;
    }
};
10200