Great Solution and Explanation!!!
Just one thing, we can avoid the extra check
if (st.empty()) longest = n;
as longest = max(longest, a); will perform that task in case the stack is empty

@sashimee !stack.empty() is a substitution for stack.size() > 0 not a substitution for stack.size() > 1.
The stack does not start off empty. If the first character is ), stack.peek() is -1 and s.charAt(stack.peek()) will be s.charAt(-1) which will cause an error I think.

Thanks for your excellent code! I did a small change, 6ms
'''
class Solution {
public:
int longestValidParentheses(string s) {
stack<int> stk;
stk.push(-1);
stk.push(0);
int maxL=0;
for(int i=1;i<s.size();i++)
{
int t=stk.top();

more specifically, in the original code, the stack.empty() && ss[stack.peek()] == ')' situation can only appear at the "root" level of braces, and there can be only one root level

yes, I think many solution that need O(n) space do not realize an very important property:
all invalid left parentheses must be at right side of all invalid right parentheses.
In fact, information those solution record is useless.

You are not using Stack and using an array.
In python, we can use array/list as a stack.
And this stack will consume less memory than the array being used by DP solutions.

This looks like a very neat solution. Could you please explain what exactly the stack keeps track of and what start keeps track of? I know what's happening but what's the physical significance in words.