Longest Valid Parentheses | LeetCode 32 | Coders Camp
Vložit
- čas přidán 26. 07. 2024
- Code: coderscamp.wixsite.com/codeit...
Link to Problem: leetcode.com/problems/longest...
This video solves the problem "Longest Valid Parentheses" in O(n) Time and space complexity
#Leetcode #coderscamp #whiteboardexplanation #codinginterview #stack #validparentheses
Excellent, clean cut explanation
good clean, short explanation.
Your explanation of algorithm is nice and straight forward. There are so many other videos making this more complicated that it needs to be. Thanks!
Thanks a lot!
really great💯💯💯
Crisp and to the point. Great !!!
nice explanation really liked it
Thank you :)
Easiest one❤️..thank you
How was this solution accepted? I don't understand the magic going on here😂
Thank you for good explaining but i want to ask why index is starting -1?
you cant iterate Stack like that , that is not allowed in stack
instead of that you can pop elements
class Solution {
public int longestValidParentheses(String s) {
if(s == null || s.length()==0){
return 0;
}
int max = 0;
Stack stack = new Stack();
for(int i=0; i
thanks
Solution wont' work as iteration over stack is wrong. Instead try this same thing just from right to left iteration
class Solution {
public int longestValidParentheses(String s) {
if(s==null || s.length()==1) return 0;
ArrayDeque stk = new ArrayDeque();
for(int i=s.length()-1;i>=0;i--){
if(!stk.isEmpty() && s.charAt(i)=='(' && s.charAt(stk.peek())==')'){
stk.pop();
}
else{stk.push(i);}
}
int index=-1,res=0;
for(int n:stk){
res=Math.max(res,n-index-1);
index=n;
}
res= Math.max(res,s.length()-index-1); //ifafter ) there is prefect brack eg:)()()
return res;
}
}