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. 使用azure send grid发送email

    1. create a send grid account 2. remember the username/password of the send grid account watermark/2 ...

  2. vim调用python格式化json数据

    vim调用python格式化json数据 November 30, 2013GNU/Linuxpython3, Vimopenwares python有个标准模块叫json,用于编码/解码,序列化/按 ...

  3. java开始到熟悉60

    本次主题:多维数组 1,多维数组的初始话有三种:默认初始化.静态初始化.动态初始化. 这里只讲解静态初始化: 这里以二位数组为例,实际应用中,一维用得最多,二维次之,三维以及三维以上几乎很少使用,而且 ...

  4. Excel实用技巧-如何批量提取excel工作表名称

    Excel实用技巧-如何批量提取excel工作表名称 1. 打开Excel文件,点击“公式”栏,进而点击“定义管理器” 2. 在弹出的对话框中,点击新增按钮, 名称:“sheet”,引用位置:“=RE ...

  5. C++ string string string string string string string string string string

    一. 初始化 string s1="i love you"; string s2(s1); //把s2初始化为string s1,注意不能写成string s2; s2(s1); ...

  6. Android应用程序窗体View的创建过程

    View类是android中非常重要的一个类.view是应用程序界面的直观体现,我们看到的应用程序界面就能够看作是View(视图)组成的. 那么我们应用程序的界面是怎么创建的呢,也就是应用程序的Vie ...

  7. mysql 查询 优化

    1.基本原则:优化数据访问 (1)是否想服务器请求了不需要的数据?提取超过需要的列,多表连接时提取所有列,提取所有列都会消耗不必要的资源,提取你所需要的列就可以了. (2)MySQL检查了太多的数据吗 ...

  8. linux getopt函数详解

    getopt(分析命令行参数)   表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const argv[ ],const ...

  9. this、call和apply、bind

    this关键字: JavaScript的this关键字,总是指向一个对象,具体指向哪个对象,是根据运行时函数指向环境动态绑定的.简单来说,this就是谁调用指向谁.具体使用中,this的指向,大致可以 ...

  10. 使用NetBeans生成jar包,并在jar包中添加资源

    在NetBeans中,执行Clean and Build便可得到jar文件 若要在jar中添加资源,先用压缩软件打开jar,然后将资源拖进当前归档文件即可 使用Class.getResource(St ...