This problem looks busy at first, but the accepted solution is built around one steady invariant. For 862. Shortest Subarray with Sum at Least K, the solution in this repository is mainly a sliding window solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: sliding window, prefix sums.
The notes already sitting in the source point us in the right direction:
- deque
- time: O(N), space: O(N)
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are shortestSubarray, P.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N), space: O(N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//deque
02//Runtime: 324 ms, faster than 43.49% of C++ online submissions for Shortest Subarray with Sum at Least K.
03//Memory Usage: 62.6 MB, less than 67.30% of C++ online submissions for Shortest Subarray with Sum at Least K.
04//time: O(N), space: O(N)
05class Solution {
06public:
07 int shortestSubarray(vector<int>& A, int K) {
08 int n = A.size();
09 //prefix sum: P[0]: 0, P[1]: A[0], P[2]: A[0]+A[1], ...
10 vector<int> P(n+1);
11
12 for(int i = 0; i < n; ++i){
13 P[i+1] = P[i] + A[i];
14 }
15
16 int ans = n+1; //impossible window size
17 deque<int> deq;
18
19 for(int y = 0; y < P.size(); ++y){
20 /*
21 for a specific y, we want to find x s.t.
22 P[y] - P[x] >= K
23
24 Motivated by that equation,
25 let opt(y) be the largest x
26 such that P[x] <= P[y] - K.
27 We need two key observations:
28
29 If x1 < x2 and P[x2] <= P[x1],
30 then opt(y) can never be x1,
31 as if P[x1] <= P[y] - K,
32 then P[x2] <= P[x1] <= P[y] - K
33 but y - x2 is smaller.
34 This implies that our candidates
35 x for opt(y) will have increasing values of P[x].
36
37 The text is saying x1 and x2,
38 why the code compare x and y??
39 */
40 //make the deque increasing
41 while(!deq.empty() && P[deq.back()] >= P[y]){
42 deq.pop_back();
43 }
44
45 while(!deq.empty() && P[deq.front()] + K <= P[y]){
46 ans = min(ans, y - deq.front());
47 /*
48 If opt(y1) = x,
49 then we do not need to consider this x again.
50 For if we find some y2 > y1 with opt(y2) = x,
51 then it represents an answer of y2 - x
52 which is worse (larger) than y1 - x.
53 */
54 deq.pop_front();
55 }
56
57 deq.push_back(y);
58 }
59
60 return ans == n+1 ? -1 : ans;
61
62 }
63};
Cost