其他分享
首页 > 其他分享> > 单调队列 monotonic queue

单调队列 monotonic queue

作者:互联网

239. Sliding Window Maximum

Hard

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:

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        Deque<Integer> deque = new LinkedList();
        int[] result = new int[nums.length-k+1];
        for(int i=0;i<nums.length;i++){
            //保持队列单调递减
            while(!deque.isEmpty() && i-deque.peekFirst()>=k)
                deque.pollFirst();
            //保持windowsize
            while(!deque.isEmpty() && nums[deque.peekLast()]<nums[i])
                deque.pollLast();
            deque.offerLast(i);
            int startInd = i-k+1;
            //队列头部值即为当前window的最大值
            if(startInd>=0) result[startInd]=nums[deque.peekFirst()];
        }
        return result;
    }
}

时间复杂度:O(N)

862. Shortest Subarray with Sum at Least K

Hard

Given an integer array nums and an integer k, return the length of the shortest non-empty subarray of nums with a sum of at least k. If there is no such subarray, return -1.

A subarray is a contiguous part of an array. 

Example 1:

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

Example 2:

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

Example 3:

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

Constraints:

class Solution {
    public int shortestSubarray(int[] nums, int k) {
        int len = nums.length;
        if(len==1) return nums[0]>=k ? 1 : -1;
        long[] sum = new long[len+1];
        sum[0]=0;
        //先计算前缀和
        for(int i=1;i<=len;i++) sum[i]=sum[i-1]+nums[i-1];
        Deque<Integer> dq = new LinkedList();
        int result = len+1;
        for(int i=0;i<=len;i++){
            //保持队列单调递增
            while(!dq.isEmpty() && sum[dq.peekLast()]>=sum[i]) 
                dq.pollLast();
            //与队列头部元素比较,如果满足条件,pop并记录结果。   为啥满足条件时可以直接pop? 因为即使i之后还有元素满足条件,也已经不是最小长度了
            while(!dq.isEmpty() && sum[dq.peekFirst()]+k<=sum[i]) {
                result = Math.min(result,i-dq.pollFirst());
            }
            dq.offerLast(i);
        }
        return result>len ? -1 : result;
    }
}

时间复杂度:O(N)

 

标签:nums,队列,monotonic,queue,int,result,Output,Input,Example
来源: https://www.cnblogs.com/cynrjy/p/15532127.html