Post

Leetcode 239. Sliding Window Maximum

Explanation for Leetcode 239 - Sliding Window Maximum, and its solution in Python.

Problem

Leetcode 239 - Sliding Window Maximum

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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] 

Approach

Visualization of the Approach:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
        l
        r
We're only saving the index in q
q = [0]

nums = [1, 3, -1, -3, 5, 3, 6, 7]
        l  r
nums[q[-1]] < nums[right] => 1 < 3, thus pop
q = []
q = [1]

nums = [1, 3, -1, -3, 5, 3, 6, 7]
        l      r
nums[q[-1]] < nums[right] => 3 < -1, ignore
q = [1, 2]
Since right + 1 >= k,
res = [3]

nums = [1, 3, -1, -3, 5, 3, 6, 7]
           l       r
nums[q[-1]] < nums[right] => -1 < -3, ignore
q = [1, 2, 3]
res = [3, 3]

nums = [1, 3, -1, -3, 5, 3, 6, 7]
               l      r
nums[q[-1]] < nums[right] => -3 < 5, pop
q = [1, 2]
nums[q[-1]] < nums[right] => -1 < 5, pop
q = [1]
nums[q[-1]] < nums[right] => 3 < 5, pop
q = []
q = [4]
res = [3, 3, 5]

nums = [1, 3, -1, -3, 5, 3, 6, 7]
                   l     r
nums[q[-1]] < nums[right] => 5 < 3, ignore
q = [4, 5]
res = [3, 3, 5, 5]

nums = [1, 3, -1, -3, 5, 3, 6, 7]
                      l     r
nums[q[-1]] < nums[right] => 3 < 6, pop
q = [4]
nums[q[-1]] < nums[right] => 5 < 6, pop
q = []
q = [6]
res = [3, 3, 5, 5, 6]

nums = [1, 3, -1, -3, 5, 3, 6, 7]
                         l     r
nums[q[-1]] < nums[right] => 6 < 7, pop
q = []
q = [7]
res = [3, 3, 5, 5, 6, 7]

Return [3, 3, 5, 5, 6, 7]

Here is the Python code for the solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        res = []
        q = deque()
        left, right = 0, 0

        # looping through nums
        while right < len(nums):
            # if q exists and nums[q[-1]] < nums[right] pop q
            while q and nums[q[-1]] < nums[right]:
                q.pop()
            
            # append right index
            q.append(right)

            # if left index is bigger than q[0]
            if left > q[0]:
                q.popleft()
            
            # if we go over k limit, append max of the sub array with size k
            if (right+1) >= k:
                res.append(nums[q[0]])
                left += 1
            right += 1
        return res    

Time Complexity and Space Complexity

Time Complexity: $O(n)$

Space Complexity: $O(n)$

This post is licensed under CC BY 4.0 by the author.