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…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 题目描述 Return the length of the shortest, non-empty, contiguous subarray of A 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…
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值,然后扫一遍sum数组. 1.首先这里不需要最大,因为刚好够k就好了 2.这里需要距离最短.就是数组的长度最短. 这里的思路也一样,不过保存很多个min值,就是用一个队列,保存前缀的min值,不需要最min,只不过有更小的就更好. 也就是如果sum数组的值是: ..... 60, 40.....Y..…
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…
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…
给定一个含有 n 个正整数的数组和一个正整数 s , 找到一个最小的连续子数组的长度,使得这个子数组的数字和 ≥  s .如果不存在符合条件的子数组,返回 0.举个例子,给定数组 [2,3,1,2,4,3] 和 s = 7,子数组 [4,3]为符合问题要求的最小长度.更多练习:如果你找到了O(n) 解法, 请尝试另一个时间复杂度为O(n log n)的解法. 详见:https://leetcode.com/problems/minimum-size-subarray-sum/descriptio…
862. 和至少为 K 的最短子数组 知识点:单调:队列:前缀和 题目描述 返回 A 的最短的非空连续子数组的长度,该子数组的和至少为 K . 如果没有和至少为 K 的非空子数组,返回 -1 . 示例 输入:A = [1], K = 1 输出:1 输入:A = [1,2], K = 4 输出:-1 输入:A = [2,-1,2], K = 3 输出:3 解法一:单调队列+前缀和 这个题目是求连续子数组,所以自然可以想到前缀和,也就是用一个数组统计到第i个位置的前缀和.所以问题就变成了 j>i &am…
题目链接 给你一个整数数组 arr 和一个整数值 target . 请你在 arr 中找 两个互不重叠的子数组 且它们的和都等于 target .可能会有多种方案,请你返回满足要求的两个子数组长度和的 最小值 . 请返回满足要求的最小长度和,如果无法找到这样的两个子数组,请返回 -1 . 一个map用来保存从0-index i 的前缀和以及索引 ------mp[前缀和] = 索引 一个dp用来保存不大于目前索引i的最小长度的子数组长度, 如果不存在, 则为maxn 用一个sum做累加, 同时对…
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th…