Problem 388602 · medium · Level 03 Linear Management & Searching

Fastest Savings Goal

sliding window · variable-size window · minimum length

deposits lists the amount you save each week (every amount is positive). Return the smallest number of consecutive weeks whose deposits add up to at least goal. If no run of weeks reaches the goal, return 0.

Examples

Input:  deposits = [2, 3, 1, 2, 4, 3], goal = 7
Output: 2
Explanation: weeks [4, 3] reach 7 in two weeks; no single week does.

Input:  deposits = [1, 4, 4], goal = 4
Output: 1

Input:  deposits = [1, 1, 1], goal = 5
Output: 0

Constraints

  • 0 <= len(deposits) <= 10**5
  • 1 <= deposits[i] <= 10**4, 1 <= goal <= 10**9
  • Target complexity: O(n) time; trying every starting week separately is too slow for the largest tests.

Goals

  • Shrink the window as aggressively as possible once the target is reached
  • Distinguish 'no valid window' from a genuine answer
Starting Python…