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] <= 1041 <= 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
Solutionis defined to encapsulate the solution logic.The
maxSlidingWindowmethod 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, andrare initialized.resultwill store the maximum elements,qis a deque (double-ended queue) that will store indices of elements in the current window in descending order of their values, andlandrrepresent the left and right boundaries of the sliding window.The code enters a while loop that continues until the right boundary
rreaches the end of the listnums.Inside the loop, there is another while loop that removes indices from the deque
qif the corresponding elements innumsare 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
ris appended to the deque usingq.append(r).The code then checks if the left boundary
lis 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 theresultlist.The left boundary
lis incremented by 1 to slide the window to the right.The right boundary
ris incremented by 1 to process the next element innums.Once the loop finishes, the
resultlist 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