An intuitive (4ms) solution, a DP solution (0ms) and an optimized clean DP solution (0ms) in C
A direct solution is to traverse the string and record the index of '(' until its corresponding ')' in a stack in which process we can get the longest for diffe
A direct solution is to traverse the string and record the index of '(' until its corresponding ')' in a stack in which process we can get the longest for different sections of valid parentheses and then using a variable max to record the current longest valid parentheses we handled so far; till the end, we return the max - the longest valid parentheses in the string. \n\n - Time Cost O(n) \n - Space Cost O(n)\n\n----------\n    //AC - 4ms;\n    int longestValidParentheses(char* s)\n    {\n        int len = strlen(s);\n        int *stack = (int*)malloc(sizeof(int)*len);\n        int top = -1;\n        stack[++top] = -1;\n        int max = 0;\n        for(int i = 0; i < len; i++)\n        {\n            int t = stack[top];\n            if(t!=-1 && s[i]==')' && s[t]=='(')\n            {\n                if(i-stack[--top] > max)\n                    max = i-stack[top];\n            }\n            else\n                stack[++top] = i;\n        }\n        return max;\n    }\n    \nUsing DP is quite simpler and easy to understand, here we can use an array maxs[] to store the longest till the current position of the string - the index of maxs[] is the same as that of the string. \n\n - When the current character is '(' then maxs[current]=0, quite intuitive; \n - when the current character is ')' we need to consider the previous state maxs[current-1] and s[current-1]: \n - at this time if s[current-1]=='(' we need further check the maxs[current-2] to get the longest valid parentheses; \n - if s[current-1]==')' then we need to check s[i-maxs[current-1]-1] and maxs[i-maxs[current-1]-2] to get the longest for maxs[current].\n\nConfused? check an example here: "()(())" if we reached the last ')', then the maxs[current-1]=2 and obviously this is not the longest for maxs[current] and to get the longest we need to check the third character which is the last character not covered by maxs[current-1] -- s[current-maxs[current-1]-1] is the character and then the maxs[current-maxs[current-1]-2] which is the first parentheses in this example.\n\n - Time Cost O(n)\n - Space Cost O(n)\n\n----------\n    //AC - 0ms - DP solution;\n    int longestValidParentheses2(char* s)\n    {\n        int len = strlen(s);\n        if(len < 2) return 0;\n        int max = 0;\n        int *maxs = (int*)malloc(sizeof(int)*len); //record the max viable length ending with the current;\n        memset(maxs, 0, sizeof(int)*len);\n        for(int i = 1; i < len; i++)\n        {\n            if(s[i] == ')')\n            {\n                if(s[i-1] == '(')\n                        maxs[i] = 2+ (i>1? maxs[i-2] : 0);\n                else if(i-maxs[i-1] > 0 && s[i-maxs[i-1]-1] == '(')\n                        maxs[i] = maxs[i-1] + (i-maxs[i-1]>1? maxs[i-maxs[i-1]-2]:0) + 2; \n                if(maxs[i] > max)\n                    max = maxs[i];\n            }\n        }\n        return max;\n    }\n    \n\n \nIn fact, just as we have analyzed in the DP solution, we can merge the two conditions s[current-1]==')' and s[current-1]=='(' altogether when s[current]==')', because if s[current-1]=='(' then maxs[current-1] will be zero which will force the second condition to be the same with the first, so we are just repeating ourselves! Here is the further optimized DP solution removing the redundancy, which is much terse and clean!\n\n - Time Cost O(n)\n - Space Cost O(n)\n\n----------\n\n    //AC - 0ms;\n    int longestValidParentheses(char* s)\n    {\n        int len = strlen(s);\n        int max = 0;\n        int *maxs = (int*)malloc(sizeof(int)*len); //record the max viable length ending with the current;\n        memset(maxs, 0, sizeof(int)*len);\n        for(int i = 1; i < len; i++)\n            if(s[i] == ')')\n            {\n                int t = i-maxs[i-1];\n                if(t>0 && s[t-1] == '(') maxs[i] = maxs[i-1] + (t>1? maxs[t-2]:0) + 2; \n                if(maxs[i] > max) max = maxs[i];\n            }\n        return max;\n    }
['Dynamic Programming']