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

Example 3:

Input: A = [2,-1,2], K = 3
Output: 3

Note:

  1. 1 <= A.length <= 50000
  2. -10 ^ 5 <= A[i] <= 10 ^ 5
  3. 1 <= K <= 10 ^ 9

Approach #1: prefix sum. [Time Limit Exceeded]

class Solution {
public:
int shortestSubarray(vector<int>& A, int K) {
int len = A.size();
if (len == 1 && A[0] >= K) return 1;
int step = INT_MAX;
vector<int> prefixSum(len, 0);
prefixSum[0] = A[0];
for (int i = 1; i < len; ++i)
prefixSum[i] = prefixSum[i-1] + A[i];
for (int i = 0; i < len; ++i) {
if (prefixSum[i] >= K)
step = min(step, i+1);
for (int j = i+1; j < len; ++j) {
if (prefixSum[j]-prefixSum[i] >= K) {
step = min(step, j-i);
}
}
}
if (step == INT_MAX) return -1;
else return step;
}
};

  

Approach #2:  deque.

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

Runtime: 144 ms, faster than 33.12% of C++ online submissions for Shortest Subarray with Sum at Least K.

Analysis:

deque Member functions

(constructor)
Construct deque container (public member function )
(destructor)
Deque destructor (public member function )
operator=
Assign content (public member function )

Iterators:

begin
Return iterator to beginning (public member function )
end
Return iterator to end (public member function )
rbegin
Return reverse iterator to reverse beginning (public member function )
rend
Return reverse iterator to reverse end (public member function )
cbegin 
Return const_iterator to beginning (public member function )
cend 
Return const_iterator to end (public member function )
crbegin 
Return const_reverse_iterator to reverse beginning (public member function )
crend 
Return const_reverse_iterator to reverse end (public member function )

Capacity:

size
Return size (public member function )
max_size
Return maximum size (public member function )
resize
Change size (public member function )
empty
Test whether container is empty (public member function )
shrink_to_fit 
Shrink to fit (public member function )

Element access:

operator[]
Access element (public member function )
at
Access element (public member function )
front
Access first element (public member function )
back
Access last element (public member function )

Modifiers:

assign
Assign container content (public member function )
push_back
Add element at the end (public member function )
push_front
Insert element at beginning (public member function )
pop_back
Delete last element (public member function )
pop_front
Delete first element (public member function )
insert
Insert elements (public member function )
erase
Erase elements (public member function )
swap
Swap content (public member function )
clear
Clear content (public member function )
emplace 
Construct and insert element (public member function )
emplace_front 
Construct and insert element at beginning (public member function )
emplace_back 
Construct and insert element at the end (public member function )

Allocator:

get_allocator
Get allocator (public member function )

Non-member functions overloads

relational operators
Relational operators for deque (function )
swap
Exchanges the contents of two deque containers (function template )
 

862. Shortest Subarray with Sum at Least K的更多相关文章

  1. [LeetCode] 862. Shortest Subarray with Sum at Least K 和至少为K的最短子数组

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  2. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

  3. [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 ...

  4. LeetCode862. 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 ...

  5. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

  6. array / matrix subarray/submatrix sum

    Maximal Subarray Sum : O(n) scan-and-update dynamic programming, https://en.wikipedia.org/wiki/Maxim ...

  7. 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...

  8. LeetCode 1099. Two Sum Less Than K

    原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and inte ...

  9. [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

随机推荐

  1. Leetcode41: Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. 《Deep Learning》全书已完稿_附全书电子版

    Deep Learning第一篇书籍最终问世了.站点链接: http://www.deeplearningbook.org/ Bengio大神的<Deep Learning>全书电子版在百 ...

  3. java 文件读写的有用工具

    java 文件读写的有用工具 package org.rui.io.util; import java.io.BufferedReader; import java.io.File; import j ...

  4. WPF新手之如何将数据绑定到TreeView

    看过许多例子,全是绑定到类的,没人说如何绑定到某个对象,偏偏我这个绝对的新手就是要绑定到一个对象,只能自己摸索了: 首先要将数据绑定到容器,有以下几个默认条件:①元数据必须包装在List或者Obser ...

  5. Drupal 7模板(主题钩子)的建议

    这一块的内容很多其它的讲的是样例.所以这里请直接稳步官方站点查看吧.链接 https://drupal.org/node/1089656

  6. mpvue——实现点击数组内的某一元素进行置顶(排序第一)操作

    前言 其实很简单只是用了js的几个函数 substr unshift splice 完整代码 | mpvue模仿QQ 代码 思路很简单,获取当前元素下标然后通过unshift函数将该值插入到数组第一位 ...

  7. easyui 日期范围前后台的设置以及实现

    1.页面部分(引入相应的js) <td class="w40 tl pl10">从日期:</td> <td> <input class=& ...

  8. 简单的JDBC编程步骤

    1.加载数据库驱动(com.mysql.jdbc.Driver) 2.创建并获取数据库链接(Connection) 3.创建jdbc statement对象(PreparedStatement) 4. ...

  9. [原创]java获取word里面的文本

    需求场景 开发的web办公系统如果需要处理大量的Word文档(比如有成千上万个文档),用户一定提出查找包含某些关键字的文档的需求,这就要求能够读取 word 中的文字内容,而忽略其中的文字样式.表格. ...

  10. CocoaPods 安装相关问题

    (1)pod install还是pod update都卡在Analyzing dependencies不动. 解决方法: 其实原因在于以上两个命令执行时会升级CocoaPods的spec仓库,加一个参 ...