Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions 239_sliding_window_maximum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
LeetCode Problem #239 — Sliding Window Maximum (Hard)
-----------------------------------------------------
You are given an array nums and an integer k.
Return the maximum value in each sliding window of size k.

Example:
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]

"""

from collections import deque

class Solution:
def maxSlidingWindow(self, nums, k):
n = len(nums)
if not nums or k == 0:
return []

dq = deque() # stores indices
result = []

for i in range(n):
# Remove elements outside the window (left side)
while dq and dq[0] < i - k + 1:
dq.popleft()

# Remove all elements smaller than current (not useful)
while dq and nums[dq[-1]] < nums[i]:
dq.pop()

# Add current element's index
dq.append(i)

# Append current window's max to result
if i >= k - 1:
result.append(nums[dq[0]])

return result


# Example usage
if __name__ == "__main__":
nums = [1,3,-1,-3,5,3,6,7]
k = 3
print("Sliding Window Maximum:", Solution().maxSlidingWindow(nums, k))