imashutosh51

Sliding Window Maximum

Oct 29th, 2022 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. '''
  2. M-1
  3. Logic:(similar to skyline problem)
  4. We will have a max heap and a map which will store the window elements as keys.
  5. first insert first window and find it's max and put into _max and answer vector.
  6. whenver you get a new number,push it into the queue,update the max and update
  7. the map.if max_element is same as the previous element before the start of window
  8. let's say prev, then it is possible that prev is not in the new window so get the
  9. maximum number present in the window using the max heap and map and update the
  10. _max and push it into the answer vector.
  11.  
  12. Insertion in a heap takes log(n) time and for all elements you are pushing so nlog(n) time complexity with O(k+N) worst time complexity
  13. '''
  14. import heapq
  15. from collections import defaultdict
  16. from typing import List
  17.  
  18. class Solution:
  19.     def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
  20.         mp = defaultdict(int)     # frequency map
  21.         heap = []                 # max heap using negatives
  22.         ans = []
  23.  
  24.         _max = nums[0]
  25.         i = 0
  26.  
  27.         # initialize first window
  28.         for i in range(k):
  29.             _max = max(_max, nums[i])
  30.             mp[nums[i]] += 1
  31.             heapq.heappush(heap, -nums[i])
  32.  
  33.         ans.append(_max)
  34.  
  35.         # slide the window
  36.         for i in range(k, len(nums)):
  37.             # add new element
  38.             heapq.heappush(heap, -nums[i])
  39.             _max = max(_max, nums[i])
  40.             mp[nums[i]] += 1
  41.  
  42.             # remove outgoing element
  43.             outgoing = nums[i - k]
  44.             mp[outgoing] -= 1
  45.  
  46.             # if outgoing was current max, clean heap
  47.             if outgoing == _max:
  48.                 while heap and mp[-heap[0]] == 0:
  49.                     heapq.heappop(heap)
  50.                 _max = -heap[0]
  51.  
  52.             ans.append(_max)
  53.  
  54.         return ans
  55. #M-2 O(n) time and O(k) space
  56. '''
  57. We are not keeping the track of the current window elements and only remove the left most element of the deque if that element is i-kth elment so can an expired element influence our answer? No,because if the i-kth element is not at the deque[0]th element means it has been already popped out of the deque.
  58. '''
  59. from collections import deque
  60. class Solution:
  61.     def maxSlidingWindow(self, arr: List[int], k: int) -> List[int]:
  62.         dq=deque()
  63.         ans=[]
  64.         for i in range(len(arr)):
  65.             if len(dq)>0 and i>=k and dq[0]==arr[i-k]:#remove the top element if it is highest of the immediate last window
  66.                 dq.popleft()
  67.  
  68.             while len(dq)>0 and dq[-1]<arr[i]: # if the number is bigger then we don't need the smaller one,because current number will stay in the window(which is moving forward) till the max time compared to already there in the window and current is already bigger number so current number will take over the max element crown now onwards only so no need of smaller elements.
  69.                 dq.pop()
  70.             dq.append(arr[i])
  71.             if i>=k-1:
  72.                 ans.append(dq[0])
  73.         return ans;
  74.  
Advertisement
Add Comment
Please, Sign In to add comment