ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

单调队列 monotonic queue

2021-11-10 10:01:42  阅读:124  来源: 互联网

标签:nums 队列 monotonic queue int result Output Input Example


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:

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104
  • 1 <= k <= nums.length
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:

  • 1 <= nums.length <= 105
  • -105 <= nums[i] <= 105
  • 1 <= k <= 109
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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有