Skip to the code
cf.
+25 XP

#45 Spending Streaks on Budget

medium

A budgeting app lists daily balance changes in spend (positive or negative). A streak is a run of back-to-back days.

Write budgetStreaks(spend, budget) that returns how many streaks have changes adding up to exactly budget. Streaks that cover different days count separately, even if they overlap.

Example 1

Input: spend = [2,3,-1,4,1], budget = 5
Output: 2

[2, 3] and [4, 1] add up to 5.

Example 2

Input: spend = [1,1,1], budget = 2
Output: 2

Days 0 and 1, and days 1 and 2, each add to 2.

Constraints

  • 0 <= spend.length <= 10^5
  • -1000 <= spend[i] <= 1000
  • -10^7 <= budget <= 10^7
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).