Leetcode - Longest Valid Parentheses (Python)

Sdílet
Vložit
  • čas přidán 2. 04. 2021
  • April 2021 Leetcode Challenge
    Leetcode - Leetcode - Longest Valid Parentheses #32
    Difficulty: Hard
  • Věda a technologie

Komentáře • 21

  • @def__init
    @def__init Před rokem +1

    props for showing the right to left example case to make it very clear for 2nd solution :)

  • @sudheerdadala3417
    @sudheerdadala3417 Před 2 lety +1

    Clean and Clear! Why does the Clicking sound excites me more ,Lol.

  • @Apurvsankhyadhar
    @Apurvsankhyadhar Před 2 lety +4

    Genius!

  • @isaiahr7041
    @isaiahr7041 Před rokem

    Is this considered twompointwrs due 5o the usage of he l and r variables?

  • @valshin
    @valshin Před rokem +2

    -1 is a mystery

    • @shashank_r94
      @shashank_r94 Před 7 měsíci

      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 !!

  • @VladBurlutsky
    @VladBurlutsky Před 3 lety

    Great!

  • @aniketwattamwar1514
    @aniketwattamwar1514 Před 3 lety +1

    thanks for this !

  • @tdog6127
    @tdog6127 Před 3 lety

    So helpful!

  • @JustMusics
    @JustMusics Před rokem

    Anyone knows why it must be -1? I tried other number seems cannot

    • @vaibhavsharma9103
      @vaibhavsharma9103 Před 6 měsíci

      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

  • @janmichaelaustria620
    @janmichaelaustria620 Před 3 lety

    This problem made me cry..... :( And on Day 3 too

  • @iam_a_sad_khan
    @iam_a_sad_khan Před 2 lety +1

    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.

  • @alejandroruiz8766
    @alejandroruiz8766 Před rokem +1

    Can you explain the -1 reason?

    • @aa10259
      @aa10259 Před rokem

      consider this case to understand--> "(())"

    • @shuyinlam8541
      @shuyinlam8541 Před rokem

      just consider ')', if the stack is empty, stack.pop() will throw error

  • @zeta563
    @zeta563 Před 2 lety

    Thanks for the helpful video. But I have question that what if s starts with several ")"? Then popping empty stack leads to error..

    • @BogdanHladiuc
      @BogdanHladiuc Před 2 lety +3

      That's why there is -1 pushed to the stack at the beginning.