You're planning to stock up on writing supplies for the upcoming school year! π
Given your budget of total dollars, you can purchase pens at cost1 dollars each and pencils at cost2 dollars each. You want to find out how many different ways you can spend your money.
Here's the twist: you can buy any quantity of each item (including zero), and you don't have to spend all your money. Each different combination of pens and pencils counts as a distinct way.
Example: If you have $20, pens cost $1, and pencils cost $2, you could buy:
- 0 pens, 0 pencils (spend $0)
- 1 pen, 0 pencils (spend $1)
- 0 pens, 1 pencil (spend $2)
- And many more combinations...
Goal: Return the total number of distinct ways to buy some combination of pens and pencils within your budget.
Input & Output
Visualization
Time & Space Complexity
We only iterate through possible pen counts once, calculating pencil combinations mathematically
Only using a few variables for calculations
Constraints
- 0 β€ total β€ 106
- 1 β€ cost1, cost2 β€ 106
- The answer will fit in a 64-bit signed integer