#2240
Number of Ways to Buy Pens and Pencils
specialist · 775 · lc medium +31 · verified · 56.6% accepted · 478 likes · top 51%
Description
You have a budget of total units. A pen costs cost1 units and a pencil costs cost2 units. You may spend any portion of your budget buying any non-negative quantity of each item.
Return the number of distinct (pen count, pencil count) combinations you can purchase.
Example 1:
Input: total = 20, cost1 = 10, cost2 = 5
Output: 9
Explanation: The price of a pen is 10 and the price of a pencil is 5.
- If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils.
- If you buy 1 pen, you can buy 0, 1, or 2 pencils.
- If you buy 2 pens, you cannot buy any pencils.
The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.
Example 2:
Input: total = 5, cost1 = 10, cost2 = 10
Output: 1
Explanation: The price of both pens and pencils are 10, which cost more than total, so you cannot buy any writing utensils. Therefore, there is only 1 way: buy 0 pens and 0 pencils.
Code
1
2
3