Find the first day a shop balance goes negative
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
disstartplus the firstdchanges. - 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]) -> intExample 1
Input
first_shortfall_day(10, [-4, -5, -3])Output
3Example 2
Input
first_shortfall_day(7, [3, -10, -1])Output
3Example 3
Input
first_shortfall_day(50, [20, -30, 10])Output
-1