Find the first day a shop balance goes negative

Write codeEasy10 min

Your task

A small shop starts with some money in its account. Each day adds that day's sales and takes away its expenses, so a day's net change can be negative.

Write first_shortfall_day(start, changes) that returns the first day the balance drops below zero.

Input and output:

  • start: the opening balance, an integer between 0 and 1,000,000,000.
  • changes: a list of up to 100,000 daily net changes, in day order. Each is an integer between -1,000,000,000 and 1,000,000,000. It may be empty.
  • Returns an integer.

Rules:

  • The balance at the end of day d is start plus the first d changes.
  • Days are numbered from 1, so changes[0] is day 1.
  • Return the first day whose balance is below zero. A balance of exactly 0 is not below zero.
  • If the balance never drops below zero, return -1.

Examples

first_shortfall_day(start: int, changes: list[int]) -> int
Example 1
Inputfirst_shortfall_day(10, [-4, -5, -3])
Output3
Example 2
Inputfirst_shortfall_day(7, [3, -10, -1])
Output3
Example 3
Inputfirst_shortfall_day(50, [20, -30, 10])
Output-1