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
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
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 )
永远渴望,大智若愚(stay hungry, stay foolish)