Day 30: Sliding Window Maximum: Sliding Window - leetcode - Python3
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.
Example 1:
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
Example 2:
Input: nums = [1], k = 1
Output: [1]
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length
SOLUTION
Time Complexity: O(n): where n is the length of the input list nums
Space Complexity:O(k): where k is the size of the sliding window.
How it works:
The class
Solution
is defined to encapsulate the solution logic.The
maxSlidingWindow
method takes two parameters:nums
, which is a list of integers, andk
, which represents the size of the sliding window. The method returns a list of integers representing the maximum elements in each sliding window.The variables
result
,q
,l
, andr
are initialized.result
will store the maximum elements,q
is a deque (double-ended queue) that will store indices of elements in the current window in descending order of their values, andl
andr
represent the left and right boundaries of the sliding window.The code enters a while loop that continues until the right boundary
r
reaches the end of the listnums
.Inside the loop, there is another while loop that removes indices from the deque
q
if the corresponding elements innums
are smaller than the current element at indexr
. This ensures that the deque only contains indices of elements that are potentially the maximum within the window. The indices are popped from the right end of the deque usingq.pop()
.After ensuring that the deque is in the correct state, the current index
r
is appended to the deque usingq.append(r)
.The code then checks if the left boundary
l
is greater than the index at the left end of the deque (q[0]
). If it is, it means that the maximum element in the window is outside the current window, so it is removed from the left end of the deque usingq.popleft()
.The code then checks if the window size is equal to or greater than
k
. If it is, it means that the window has reached the desired size. In this case, the maximum element in the current window is obtained by accessingnums[q[0]]
, and it is appended to theresult
list.The left boundary
l
is incremented by 1 to slide the window to the right.The right boundary
r
is incremented by 1 to process the next element innums
.Once the loop finishes, the
result
list containing the maximum elements in each sliding window is returned.
In summary, the code efficiently finds the maximum element in each sliding window of size k
by using a deque to keep track of potentially maximum elements. It slides the window through the input list and updates the deque accordingly to maintain the maximum elements within the window.
Comments
Post a Comment