Created
May 16, 2017 16:24
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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