Skip to the code
cf.
+50 XP

#274 Shortest Burst Over Budget

hard

A cloud billing page tracks cost changes per minute in changes; a change can be negative when a credit is applied. An alert fires on the shortest burst of consecutive minutes whose changes add up to at least budget.

Write shortestBurst(changes, budget) that returns the length of the shortest non-empty run of consecutive minutes with a sum of at least budget, or -1 if no run reaches it.

Example 1

Input: changes = [2,-1,2], budget = 3
Output: 3

Only the whole run 2 - 1 + 2 = 3 reaches 3.

Example 2

Input: changes = [1,2], budget = 4
Output: -1

The total is only 3.

Constraints

  • 1 <= changes.length <= 10^5
  • -10^5 <= changes[i] <= 10^5
  • 1 <= budget <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).