From 8f9b37a760ccac5c9ffb0a78f52477f7e1c9e902 Mon Sep 17 00:00:00 2001 From: Harshit Kumar Pandey Date: Thu, 16 Oct 2025 19:46:02 +0530 Subject: [PATCH] Add sliding window maximum solution Implement solution for LeetCode Problem #239, which finds the maximum value in each sliding window of size k in an array. --- 239_sliding_window_maximum.py | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 239_sliding_window_maximum.py diff --git a/239_sliding_window_maximum.py b/239_sliding_window_maximum.py new file mode 100644 index 0000000..c541480 --- /dev/null +++ b/239_sliding_window_maximum.py @@ -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))