Iam_Sandeep

Untitled

Jan 31st, 2026
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. class Solution:
  2.     def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
  3.         from collections import deque
  4.         q = deque()
  5.         ans = []
  6.         for r in range(len(nums)):
  7.             while q and q[0]<=r-k:
  8.                 q.popleft()
  9.             while q and nums[q[-1]]<nums[r]:
  10.                 q.pop()
  11.             q.append(r)
  12.             if r>=k-1:
  13.                 ans.append(q[0])
  14.         return [nums[i] for i in ans]
Advertisement
Add Comment
Please, Sign In to add comment