[LeetCode] 239. Sliding Window Maximum (Hard) (JAVA) One question per day

[LeetCode] 239. Sliding Window Maximum (Hard) (JAVA)

Title address: https://leetcode.com/problems/sliding-window-maximum/

Title description:

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]

Example 3:

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

Example 4:

Input: nums = [9,11], k = 2
Output: [11]

Example 5:

Input: nums = [4,-2], k = 2
Output: [4]

Constraints:

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

General idea

Given an array nums, a sliding window of size k moves from the leftmost side of the array to the rightmost side of the array. You can only see the k numbers in the sliding window. The sliding window only moves one position to the right at a time.

Returns the maximum value in the sliding window.

Advanced:

Can you solve this problem in linear time complexity?

Problem-solving method

  1. If the time complexity is O(n), the violent method must not be used, and the time complexity of the violent method is O(kn)
  2. Using the two-way queue method, the maximum value is stored at the top of the queue, and the last value smaller than the current value is removed from the queue before each value is stored, so that the queue is sorted from largest to smallest ( LinkedList is used here)
  3. When the window is sliding, you need to remove the top value of the previous window. If the removed value is just the top value of the queue, that is, the maximum value, you also need to remove the value in front of the queue, and then take the top value of the queue. Is the maximum value of the current window
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (k == 1) return nums;
        int[] res = new int[nums.length - k + 1];
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < nums.length; i++) {
            if (i >= k && nums[i - k] == list.get(0)) list.removeFirst();
            while (list.size() > 0 && list.getLast() < nums[i]) {
                list.removeLast();
            }
            list.add(nums[i]);
            if (i >= k - 1) res[i - k + 1] = list.get(0);
        }
        return res;
    }
}

Execution time: 34 ms, defeating 57.94% of Java users
Memory consumption: 50 MB, defeating 72.20% of Java users

Welcome to pay attention to my official account, LeetCode updates one question every day

Guess you like

Origin blog.csdn.net/qq_16927853/article/details/112093630