Leetcode 239 Solution

This article provides solution to leetcode question 239 (sliding-window-maximum)

https://leetcode.com/problems/sliding-window-maximum

Solution

class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: s = deque() ans = [] for i, num in enumerate(nums): while s and s[0][1] <= i - k: s.popleft()
while s and s[-1][0] < num: s.pop() s.append((num, i))
if i >= k - 1: ans.append(s[0][0]) return ans