Skip to content

Instantly share code, notes, and snippets.

@agaase
Created May 16, 2017 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agaase/a9c530829ca10bfdfe79ec1d635c857b to your computer and use it in GitHub Desktop.
Save agaase/a9c530829ca10bfdfe79ec1d635c857b to your computer and use it in GitHub Desktop.
Given a string consisting of opening and closing parenthesis, find length of the longest valid parenthesis substring.
//http://practice.geeksforgeeks.org/problems/longest-valid-parentheses/0
var seq = [1,0,1,0,1,1]
var counts = [];
var stack = [];
for(var i=seq.length-1;i>=0;i--){
var val = seq[i];
if(val){
stack.push(val);
}
if(!val){
if(stack.pop()){
if(counts.length){
counts[counts.length-1] +=2;
}else{
counts.push(2);
}
}else{
counts.push(0);
}
}
}
console.log(counts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment