239. Sliding Window Maximum (Hard) - TengnanYao/daily_leetcode GitHub Wiki
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
stack = []
for i in range(k):
num = nums[i]
while stack and num > stack[-1]:
stack.pop()
stack.append(num)
result = [stack[0]]
for i in range(k, len(nums)):
pre = nums[i - k]
if pre == stack[0]:
stack.pop(0)
num = nums[i]
while stack and num > stack[-1]:
stack.pop()
stack.append(num)
result.append(stack[0])
return result