题目地址:
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.
If there is no non-empty subarray with sum at least K, return -1.
分析:
画出一个直角坐标系,横轴为数组下标idx,纵轴为subarray[0,idx]的和。
坐标系中的点连成曲线,如下图:
使用滑动窗口,窗口左侧不会是在黄框区域(右侧可能会在)。
代码:
见上面网址中的 Solutin。
有个技巧是使用deque。