Sliding Window Maximum | Leetcode

Поділитися
Вставка
  • Опубліковано 8 лис 2024

КОМЕНТАРІ • 85

  • @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

  • @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.

  • @ItsZarif
    @ItsZarif Рік тому +2

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

  • @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)

  • @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

  • @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.

  • @jenny911224
    @jenny911224 Рік тому +1

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

  • @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

  • @roshanraut3093
    @roshanraut3093 3 роки тому +2

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

  • @aravindravva3833
    @aravindravva3833 Рік тому +1

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

  • @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.

  • @umashankarboga6283
    @umashankarboga6283 3 роки тому

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

  • @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???

  • @23cash86
    @23cash86 3 місяці тому

    Wonderful explanation

  • @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)

  • @tanvirahmed7993
    @tanvirahmed7993 3 роки тому +2

    You are a genius!

  • @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!! 🤗😊

  • @janaSdj
    @janaSdj 5 місяців тому

    Nice explanation , thank you;

  • @fakruu
    @fakruu 3 роки тому +2

    Top notch explanation !! thanks for the efforts

  • @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?

  • @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

  • @sciphilo754
    @sciphilo754 3 роки тому +4

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

  • @_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)

  • @pranavsharma7165
    @pranavsharma7165 3 роки тому +2

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

  • @shivamsingh7219
    @shivamsingh7219 3 роки тому +2

    What is the time complexity of DLL implementation

  • @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

  • @arshadjamal5405
    @arshadjamal5405 3 роки тому +3

    Thank you so much for such a valuable content

  • @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?

  • @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]

  • @pratibhashetty5277
    @pratibhashetty5277 3 роки тому

    Best Explanation!

  • @madhusaivemulamada3556
    @madhusaivemulamada3556 3 роки тому

    Nice explanation

  • @devanshkumar1542
    @devanshkumar1542 2 роки тому

    SUBARAAAAASHI exparranasan

  • @MadForCs16
    @MadForCs16 3 роки тому +1

    I'm grateful. Ty.

  • @mondayemmanuel191
    @mondayemmanuel191 3 роки тому

    Another great explanation. Thanks.

  • @andriidanylov9453
    @andriidanylov9453 9 місяців тому

    Thank You

  • @gurubalaji5611
    @gurubalaji5611 3 роки тому

    Wonderful explanation..

  • @malarkodim9217
    @malarkodim9217 2 роки тому +1

    Thank you!!

  • @Live-hh6li
    @Live-hh6li 3 роки тому

    Great explanation

  • @athletics365
    @athletics365 3 роки тому +1

    Great explanation!

  • @De1n1ol
    @De1n1ol 2 роки тому +1

    what is the name of software you use to draw?

  • @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??

  • @AkashKumar-ym4gu
    @AkashKumar-ym4gu 3 роки тому +2

    Thank You Sir..🙏🙏

  • @ShiviKumar-z2q
    @ShiviKumar-z2q Рік тому

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

  • @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).

  • @mdiqbalmahmud8233
    @mdiqbalmahmud8233 3 роки тому +1

    thank you sir

  • @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?

  • @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