Sliding Window Maximum | Leetcode

Поделиться
HTML-код
  • Опубликовано: 27 окт 2024

Комментарии • 85

  • @ItsZarif
    @ItsZarif Год назад +2

    I watched 5 different videos but yours was the only one that helped me finally understand the deque solution. Thank you!

  • @srikrishnarohanmadiraju8688
    @srikrishnarohanmadiraju8688 3 года назад +16

    Thanks! Very comprehensive explanation.
    In the third approach using Doubly-LL, I understand that the time complexity is O(N) due to the traversal of the array.
    Is there no time consumption for the building of DLL. If the elements are coming in descending order, then we have to build the window size right, so wouldn't that make the complexity O(N+K) (K amortized) . Can you elaborate it a bit more on the time complexity arising during the building of DLL and how it impacts the overall complexity.

  • @WdnUlik2no
    @WdnUlik2no 2 года назад +1

    Thank you! I got 57/61 correct, but kept getting Time limited exceeded for the last 4 test cases. When I tried switching to Priority queue implementation, my correct went down to 49/61, still getting Time Limit exceded. This has been driving me crazy ALL day.

  • @shekharchaudhary2161
    @shekharchaudhary2161 3 года назад +13

    I have watched two of your videos today and I can tell you that the quality of your teaching has increased. Just after understanding both the approaches I felt that I could code on my own and so I did. Thank you very much.

    • @techdose4u
      @techdose4u  3 года назад +3

      Welcome :) Thanks for the feedback

  • @siobhanahbois
    @siobhanahbois 3 года назад +52

    Brute force: 2:22
    Heap: 4:21
    Deque: 12:58

    • @techdose4u
      @techdose4u  3 года назад +6

      Thanks for putting timestamp 😅

    • @zhhey
      @zhhey 3 года назад +8

      all heroes dont wear capes

  • @ahmadalk2540
    @ahmadalk2540 3 года назад +2

    Just honestly one think I didn't like is that with the method fo dequeue:
    I actually watched the video in order to understand how this method works but you came up with these steps rather than showing the actual way of problem solving and how we can figure out such steps in similar problems.
    Thank you,

    • @techdose4u
      @techdose4u  3 года назад +1

      Thanks for your feedback. The logic is similar to maintaing non-decreasing or non-increasing order using stack. We used deque to access from multiple ends.

    • @ahmadalk2540
      @ahmadalk2540 3 года назад +1

      @@techdose4u would you please help me by suggesting an easier similar problem in order to get a better understanding with this method.

    • @techdose4u
      @techdose4u  3 года назад +1

      @@ahmadalk2540 easier similar problem can be stock span problem (but that is not window based)

  • @jenny911224
    @jenny911224 Год назад +1

    The best video for this leetcode question and it's so easy to follow! THANK YOU!

  • @roshanraut3093
    @roshanraut3093 3 года назад +2

    The explanation was simply Makkhan ie. Butter..... Hat's off...............

  • @yitingg7942
    @yitingg7942 3 года назад +3

    Thank you so much Sir! Finally understood it watching the graph you drew. The third solution is so hard to think of..

    • @techdose4u
      @techdose4u  3 года назад

      Nice :)

    • @yitingg7942
      @yitingg7942 3 года назад +2

      @@techdose4u Sir can I ask a question? I was also trying to solve it using heap, but I realized that by using heap there's no way I only store Integer in the heap, is this correct? However by using deque, you can store only Integer( index ) in the Deque like your other video. But by heap, is has to be a node but not just a index , right ?

    • @techdose4u
      @techdose4u  3 года назад +1

      @@yitingg7942 Yes right. Position info is also essential to maintain.

    • @yitingg7942
      @yitingg7942 3 года назад

      @@techdose4u You replied so fast!! Thank you so much Sir! Happy Lunar New Year!! 🤗😊

  • @beaconbecay1648
    @beaconbecay1648 3 года назад +2

    Thank you for the great explanation as usual TECH DOSE. The deque solution does not appear to exactly be O(N). For instance, consider the input: nums = [3, 1, -1, 0, 2, 0, -6, -7] and k = 4. You will notice that you have to pop multiple values (not just one) when you reach index 4. And this can also happen several times in different types of inputs.

    • @techdose4u
      @techdose4u  3 года назад +2

      Every node is pushed and popped exactly once. So, Dequeue time is only O(1).

    • @robertkoufax1132
      @robertkoufax1132 2 года назад

      @@techdose4u Not really. When maintaining the deque in DSC order, current selected element (right element in k sliding window) need to repeatedly compare to the rear element in this deque, regardless of its data structure being Doubly-LL.

  • @dummydp2762
    @dummydp2762 10 месяцев назад

    In the heap approach, what is the heap size we should declare.
    If its n, then the space complexity will be O(n)..
    Or should we declare the heap of size 2k..?
    If, we declare heap array of size 2k, then for each heapify, it will take log2k times..
    Please clarify once on the heap size what should we declare.. Thanks

  • @aravindravva3833
    @aravindravva3833 Год назад +1

    why to keep index in heap?
    because the TC for removal is O(n)
    and for push,pop is O(logn)

  • @sciphilo754
    @sciphilo754 3 года назад +4

    Thanks a ton for this valuable content!! Top notch explanation.

  • @789dj1
    @789dj1 Год назад

    This was an amazing video. My question is would it effect the run time if you removed from the heap as you went through the window? I dont believe it would. Also If you use that approach their wouldnt be a need for the index right?

  • @_ityadi
    @_ityadi 3 года назад +2

    Can you explain why the time complexity for Deque solution is O(n)...are we not count the push operation? The push operation would at worst requires us to traverse through the whole linked list right? So, the worst case of K traversal should be counted. So shouldn't it be O(nk) ??? Please help

    • @hectorg362
      @hectorg362 Год назад

      The way I see it, at worst case the window is the size of the array, which is why it would be O(n)

  • @23cash86
    @23cash86 2 месяца назад

    Wonderful explanation

  • @fakruu
    @fakruu 3 года назад +2

    Top notch explanation !! thanks for the efforts

  • @pranavsharma7165
    @pranavsharma7165 3 года назад +2

    Amazing Explanation! Thanks a lot....!!

  • @user0xf
    @user0xf 3 года назад +4

    I'm not sure that 2nd algorithm is O(N). We traverse list once, correct. But for every position we keep popping elements which are less then current - this is a linear operation too.
    Am I missing something?

    • @techdose4u
      @techdose4u  3 года назад +5

      You will push once and pop once (maximum time) for each element. Once an element is pushed and popped then it's never pushed again. So, operations is in order of O(N).

    • @DiegoMorales-iy7fw
      @DiegoMorales-iy7fw 3 года назад +1

      @@techdose4u Thank you for the explanation!

    • @techdose4u
      @techdose4u  3 года назад +1

      @@DiegoMorales-iy7fw welcome

    • @MadForCs16
      @MadForCs16 3 года назад

      @@techdose4u This comment has more knowledge than most of the youth. Thank you. It was extremely helpful.

    • @KrishnaYadav-kw9yq
      @KrishnaYadav-kw9yq Год назад

      Thanks for clarity was having same doubt

  • @janaSdj
    @janaSdj 4 месяца назад

    Nice explanation , thank you;

  • @umashankarboga6283
    @umashankarboga6283 3 года назад

    same question has been asked recently in quotient technologies last month....!!!

  • @tanvirahmed7993
    @tanvirahmed7993 3 года назад +2

    You are a genius!

  • @AdityaSingh-lf7oe
    @AdityaSingh-lf7oe 3 года назад +2

    A map implementation is easier than heap implementation.. All elements in map are sorted just like in heap, but popping the leftmost index of window is much easier in map (map[nums[i-k] -= 1)

  • @pkboolean
    @pkboolean 3 года назад +2

    well explained, but you should take pauses when you speak, it is sometimes difficult to follow along as you are talking nonstop

    • @techdose4u
      @techdose4u  3 года назад +2

      You can pause I guess 😅 otherwise time will increase and hurt the retention time

  • @shwetaravi2696
    @shwetaravi2696 3 года назад +1

    i just have a doubt like in deque ,method code you have mentioned should we add another for loop before to add the first 3 elements in the vector?or am i missing something?

  • @arshadjamal5405
    @arshadjamal5405 3 года назад +3

    Thank you so much for such a valuable content

  • @De1n1ol
    @De1n1ol 2 года назад +1

    what is the name of software you use to draw?

  • @MadForCs16
    @MadForCs16 3 года назад +1

    I'm grateful. Ty.

  • @devanshkumar1542
    @devanshkumar1542 2 года назад

    SUBARAAAAASHI exparranasan

  • @tejajuluri3489
    @tejajuluri3489 3 года назад +1

    We can use increase key and decrease key operation ,by comparing elements which are being poped and pushed... if the element that is gng to be pushed greater than popped element we use increase key operation ,if they r equal we do ntg else decrease key operation???

  • @shivamsingh7219
    @shivamsingh7219 3 года назад +2

    What is the time complexity of DLL implementation

  • @vitaliizinchenko556
    @vitaliizinchenko556 3 года назад +1

    I don't understand how Heap solution has O(n logk)? I think you are loosing the fact that heap removal is taking O(k). So, for each N you will remove item from the heap which will lead to a solution of O(n k).

  • @pratibhashetty5277
    @pratibhashetty5277 3 года назад

    Best Explanation!

  • @ShiviKumar-z2q
    @ShiviKumar-z2q Год назад

    Not able to access the code links, Error 404..
    pls check your all code links

  • @partharanke5925
    @partharanke5925 2 года назад

    I think instead of using deque only two variables first_maximum and second_maximum are sufficient., do you think that it will work??

    • @Apoorvpandey
      @Apoorvpandey 2 года назад

      I think it wont work that way

  • @malarkodim9217
    @malarkodim9217 2 года назад +1

    Thank you!!

  • @mondayemmanuel191
    @mondayemmanuel191 3 года назад

    Another great explanation. Thanks.

  • @andriidanylov9453
    @andriidanylov9453 8 месяцев назад

    Thank You

  • @madhusaivemulamada3556
    @madhusaivemulamada3556 3 года назад

    Nice explanation

  • @derilraju2106
    @derilraju2106 Год назад

    Two methods solutuon, sliding window and heap
    from collections import deque
    class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
    return self.heap_method(nums,k)
    # sliding window is O(N) method
    i = 0
    j = 0
    que = deque()
    ans = []
    while j < len(nums):
    while que and nums[j] > que[-1]:
    que.pop()
    que.append(nums[j])
    if j-i+1 < k:
    j+=1
    elif j-i+1 == k:
    ans.append(que[0])
    if que[0] == nums[i]:
    que.popleft()
    i+=1
    j+=1
    return ans
    def heap_method(self,nums,k):
    # O(NlgK)
    nums = [-i for i in nums]
    pq = [(val,idx) for idx,val in enumerate(nums[:k])]
    heapify(pq)
    res = [-pq[0][0]]
    for i in range(k,len(nums)):
    while pq and pq[0][1]

  • @athletics365
    @athletics365 3 года назад +1

    Great explanation!

  • @gurubalaji5611
    @gurubalaji5611 3 года назад

    Wonderful explanation..

  • @Live-hh6li
    @Live-hh6li 3 года назад

    Great explanation

  • @AkashKumar-ym4gu
    @AkashKumar-ym4gu 3 года назад +2

    Thank You Sir..🙏🙏

  • @mdiqbalmahmud8233
    @mdiqbalmahmud8233 3 года назад +1

    thank you sir

  • @TomerBenDavid
    @TomerBenDavid 3 года назад

    Is it samson qu2 mic?

  • @tanvirahmed7993
    @tanvirahmed7993 3 года назад +1

    Do you have the JAVA code?

  • @saskirakosyan5268
    @saskirakosyan5268 Год назад

    Horrible accent ((( but topic is good

  • @22222222222222223464
    @22222222222222223464 Год назад

    Hi! What I don't understand is why we still have the (1,0) node in the graph when we moved to the right in the heap solution. I understand it wasn't the max and it didn't affect the result but what if it will over time?