> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Sliding Window Maximum Python Solution

> Tested Python solution for LeetCode 239 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 239, Hard. Topics: Array, Queue, Sliding Window, Heap (Priority Queue), Monotonic Queue. [View on LeetCode](https://leetcode.com/problems/sliding-window-maximum/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 239   # by problem number
lcpy gen -s sliding_window_maximum   # by problem name
```

## Problem

You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position.

Return *the max sliding window*.

### Examples

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

**Explanation:**

Window position                Max

***

\[1  3  -1] -3  5  3  6  7       **3**
1 \[3  -1  -3] 5  3  6  7       **3**
1  3 \[-1  -3  5] 3  6  7      \*\* 5\*\*
1  3  -1 \[-3  5  3] 6  7       **5**
1  3  -1  -3 \[5  3  6] 7       **6**
1  3  -1  -3  5 \[3  6  7]      **7**

```
Input: nums = [1], k = 1
Output: [1]
```

### Constraints

* 1 \<= nums.length \<= 10^5
* -10^4 \<= nums\[i] \<= 10^4
* 1 \<= k \<= nums.length

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sliding_window_maximum/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sliding_window_maximum/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque


class Solution:
    # Time: O(n)
    # Space: O(k)
    def max_sliding_window(self, nums: list[int], k: int) -> list[int]:
        result: list[int] = []
        # Store indices of elements in decreasing order of values
        dq: deque[int] = deque()

        for i, num in enumerate(nums):
            # Remove indices that are out of the current window
            while dq and dq[0] <= i - k:
                dq.popleft()

            # Remove indices whose corresponding values are less than the current value
            while dq and nums[dq[-1]] < num:
                dq.pop()

            # Add current index
            dq.append(i)

            # Add maximum to result when we have a complete window
            if i >= k - 1:
                result.append(nums[dq[0]])

        return result
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(k)  |

## Tags

[Grind](/catalog/grind), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
