Monotonic Stack Data Structure Explained

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • LeetCode Pattern - Monotonic stack, Monotonic deque, Monotonic queue
    Next Greater Element II
    Daily Temperature
    Sliding Window Maximum
    ☑️Full solution: algo.monster/problems/valid_p...
    🟪 Check out AlgoMonster: algo.monster
    🥷 Discord: / discord
    #100secondsofcode #leetcode #leetcodesolution #python #coding #programming
    Intro and intuition: (0:00)
    Example walk through: (0:35)
    Implementation: (4:26)
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Komentáře • 58

  • @user-cb1si7ig6p
    @user-cb1si7ig6p Před rokem +136

    Very well explained, would be easier to learn without the moving background.

    • @JackHou-vw7hs
      @JackHou-vw7hs Před rokem +8

      I loved the moving background. It helped me stay focused

    • @bluesteel1
      @bluesteel1 Před 9 měsíci +24

      Everything was fine before i read your comment. Now i cant ignore it .

    • @jigar2238
      @jigar2238 Před 9 měsíci +3

      @@bluesteel1 😆

    • @siddhantprakash.
      @siddhantprakash. Před 5 měsíci +2

      @@bluesteel1 😂

  • @KuaisArts
    @KuaisArts Před rokem +3

    Wow! Thanks for walking through the example, it was very insightful

  • @PssGameplayER
    @PssGameplayER Před 11 měsíci +5

    first time heard about Monotonic Stack, thanks for explaining it so properly

  • @sharwariphadnis1298
    @sharwariphadnis1298 Před 10 měsíci

    Awesome video! Easy to understand

  • @biswajeetpadhi100
    @biswajeetpadhi100 Před rokem +17

    the background of this video is greatly designed. its very easy to focus with this background in motion

    • @algo.monster
      @algo.monster  Před rokem +15

      can't tell if it's positive or sarcasm haha but yeah glad you like it!

    • @biswajeetpadhi100
      @biswajeetpadhi100 Před rokem +5

      @@algo.monster the background in motion acts as a boundary to the actual content, so I think it really works.

    • @szymonpiechutowski2340
      @szymonpiechutowski2340 Před rokem +8

      I am a complete opposite to this idea, I feel like this 🤮 when I see it. It is not optimal for every 🧠

  • @yoDQ
    @yoDQ Před 6 měsíci +2

    Thank you for the monotonic stack explainer. It may be helpful to also show an image of the heights array above the image of the answer array to help visualize the relationship between the two better while popping and inserting.

  • @aleksandr_t
    @aleksandr_t Před rokem

    Great video, thank you a lot!

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

    wow very good explanation. thank you..

  • @LunaOoze
    @LunaOoze Před 2 měsíci

    Perfect explanation, thank you !

  • @hardiksinghal1360
    @hardiksinghal1360 Před měsícem

    Awesome explaination

  • @helloguys9201
    @helloguys9201 Před rokem

    Wonderful Explanation and PPT's

  • @ignassablinskas9175
    @ignassablinskas9175 Před 3 měsíci

    Very nice explanation, great job.

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

    So helpful 😭🖤

  • @sandeepamarnath3295
    @sandeepamarnath3295 Před 8 měsíci +3

    Great video thanks. Trying to understand how the monotonic stack approach would be of O(n) time. We traverse the array only once for sure, but what about the pop operations? For a few elements of array, we are popping out of stack multiple times until we find a stack element that is greater than the current element, so are we not counting this towards the time complexity? If length of stack is k, wouldn't the overall time be O(nk)? Thanks again!

    • @algo.monster
      @algo.monster  Před 8 měsíci +7

      Every element in the array is processed exactly once. When an element is popped from the stack, it does not re-enter. Consequently, the maximum number of operations is 2n, accounting for each element being pushed and popped once.

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

    would you be kind enough to share what is the application that you are using for the walk through of the solution :) Thank you in advance.

  • @bluesteel1
    @bluesteel1 Před 9 měsíci

    Nice

  • @AINikunjGour
    @AINikunjGour Před rokem

    You are Osm bro..

  • @dhruvsakariya3129
    @dhruvsakariya3129 Před 10 měsíci +1

    you made it very easy by making visualizing approach. Thanks

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

    Before learning this, I'd just use a segment tree for the first question xD.

  • @PhuocOngGia
    @PhuocOngGia Před 9 měsíci

    bro is god

  • @alexsinx
    @alexsinx Před 8 měsíci

    Why is the time complexity O(n) if we have two nested loops? shouldn't it be o(n*m) where n is the number of elements in the array and m is the number of elements in the stack?

    • @algo.monster
      @algo.monster  Před 8 měsíci

      The code can be indeed deceiving. But remember each element enters or exits at most once. This is why monotonic stack is a efficient data structure.

  • @user-pu1mf8px5e
    @user-pu1mf8px5e Před 6 měsíci +2

    The background is too distracting

  • @codemonkey007
    @codemonkey007 Před 3 měsíci

    Why the solution iterative the array from right side to left side, is this order matter?

    • @TragicGFuel
      @TragicGFuel Před 13 dny

      The order is a choice, you could have used a strictly increasing function instead

  • @parulgarg5362
    @parulgarg5362 Před 9 měsíci

    Thanks for the video! Feedback on the moving background : VERY DISTURBING

  • @dimahodan232
    @dimahodan232 Před rokem

    why brute force is O(n^2)? Each element doesn't start from the beginning of the array.
    Isn't it O(n*m) where m is i - k - 1? where k is the current element.

    • @algo.monster
      @algo.monster  Před rokem

      k is not an input parameter. we have to express it in n. after dropping the content it's n^2

    • @douglas5260
      @douglas5260 Před rokem +2

      its n-1 + n-2 +n-3 ... = n**2

    • @BurtPredrag
      @BurtPredrag Před rokem

      @@douglas5260 its not multiplication it should be addition

    • @douglas5260
      @douglas5260 Před rokem

      @@BurtPredrag Yeah, I've corrected it. Thanks!

  • @RahulBansal14
    @RahulBansal14 Před 2 měsíci

    The moving background make it hard to follow.

  • @googleit2490
    @googleit2490 Před 10 měsíci +2

    New Topic Learned!!
    Sep'11, 2023 06:36 pm

  • @kaszatus2
    @kaszatus2 Před 7 měsíci +2

    Literally the worst sample, because not much of exploration... Maybe thats why for you its most confusing... As so its this video...

  • @bogdan206
    @bogdan206 Před 7 měsíci +2

    bro remove that background!

  • @tiger7858
    @tiger7858 Před rokem +9

    very bad background