props for showing the right to left example case to make it very clear for 2nd solution :)
@valshin2 жыл бұрын
-1 is a mystery
@shashank_r9411 ай бұрын
Two reasons: 1. Stack can't be empty - In case all parenthesis are valid you need the first (leftmost) index to find the length Cases - "()" or "()()()((" 2. using (-1) instead of 0 (let's say) then ( i - stack[-1]) will give an incorrect result while subtracting with 0 for "()" will give you 1-0 = 1 which instead should be 2(1- (-1)). Hope it helps !!
@sudheerdadala34173 жыл бұрын
Clean and Clear! Why does the Clicking sound excites me more ,Lol.
@isaiahr7041 Жыл бұрын
Is this considered twompointwrs due 5o the usage of he l and r variables?
@Apurvsankhyadhar3 жыл бұрын
Genius!
@alejandroruiz87662 жыл бұрын
Can you explain the -1 reason?
@aa102592 жыл бұрын
consider this case to understand--> "(())"
@shuyinlam8541 Жыл бұрын
just consider ')', if the stack is empty, stack.pop() will throw error
@JustMusics Жыл бұрын
Anyone knows why it must be -1? I tried other number seems cannot
@vaibhavsharma910311 ай бұрын
what if we have ')' in the starting in this case you will try to pop() and stack is empty so it will give error so we push -1 before we start traversing
@iam_a_sad_khan2 жыл бұрын
I feel so stupid after seeing these solutions. I was able to solve this problem using dp, and my solution ran in 0ms, but my solution is so complicated compared to these solutions.
@janmichaelaustria6203 жыл бұрын
This problem made me cry..... :( And on Day 3 too
@timc34063 жыл бұрын
Hang in there!
@aniketwattamwar15143 жыл бұрын
thanks for this !
@timc34063 жыл бұрын
My pleasure
@VladBurlutsky3 жыл бұрын
Great!
@zeta5632 жыл бұрын
Thanks for the helpful video. But I have question that what if s starts with several ")"? Then popping empty stack leads to error..
@BogdanHladiuc2 жыл бұрын
That's why there is -1 pushed to the stack at the beginning.