leetcode 862 shorest subarray with sum at least K

https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/

首先回顾一下求max子数组的的方法是:记录一个前缀min值,然后扫一遍sum数组。

1、首先这里不需要最大,因为刚好够k就好了

2、这里需要距离最短。就是数组的长度最短。

这里的思路也一样,不过保存很多个min值,就是用一个队列,保存前缀的min值,不需要最min,只不过有更小的就更好。

也就是如果sum数组的值是:

..... 60, 40.....Y.....

那么在原本的队列中,60可以pop出来了,因为他被40代替了,因为Y减去40的值肯定比60大,更有可能大于k,而且这样距离也更短。

class Solution {
public:
    int shortestSubarray(vector<int> A, int k) {
        int len = (int)A.size();
        deque<int> que;
        vector<int> sum(len + 1, 0);
        for (int i = 0; i < len; ++i) {
            sum[i + 1] = sum[i] + A[i];
        }
        int ans = len + 1;
        que.push_back(0);
        for (int i = 1; i <= len; ++i) {
            while (que.size() > 0 && sum[i] - sum[que.front()] >= k) {
                ans = min(ans, i - que.front());
                que.pop_front();
            }
            while (que.size() > 0 && sum[i] <= que.back()) {
                que.pop_back();
            }
            que.push_back(i);
        }
        return ans == len + 1 ? -1 : ans;
    }
} t;

原文地址:https://www.cnblogs.com/liuweimingcprogram/p/9858641.html

时间: 2024-07-31 05:37:26

leetcode 862 shorest subarray with sum at least K的相关文章

lc 862. Shortest Subarray with Sum at Least K

断网导致原来写的那么多答案全没了,博客园能不能实时保存草稿,醉. https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 给一个数组a,找和大于k的所有子数组中的最短的那个. 最近二分有点上头,因为的确很强大,两个考虑二分的情况(其实需要刻意去想想能不能使用二分才会想到,这会是个好习惯): 1.最优问题变判断问题.比如:行递增列递增的矩阵中找某个元素->行递增列递增的矩阵中找第k大元素 2.有相应的简单孪生问题.比

862. Shortest Subarray with Sum at Least K

Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there is no non-empty subarray with sum at least K, return -1. Example 1: Input: A = [1], K = 1 Output: 1 Example 2: Input: A = [1,2], K = 4 Output: -1 Exa

[Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K

Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there is no non-empty subarray with sum at least K, return -1. Example 1: Input: A = [1], K = 1 Output: 1 Example 2: Input: A = [1,2], K = 4 Output: -1 Exa

LeetCode之Maximum Subarray

Maximum Subarray的通过率居然这么高,我提交了几次都是Wrong Answer,郁闷! Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has t

[LeetCode] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

[LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: 0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be