Ctrl K

Fewest Bills

Medium
A cash register holds bills of several denominations, listed in the array denominations, with an unlimited supply of each. You need to pay out exactly total. Return the smallest number of bills that does it. If the exact sum cannot be made, return −1. When total is 0, the answer is 0.
Example 1
denominations=[4, 3, 10], total=13 → 2

13 = 10 + 3, so two bills are enough.

Example 2
denominations=[6, 9], total=16 → -1

Any sum of 6s and 9s is divisible by 3, but 16 is not, so the answer is −1.

Example 3
denominations=[1, 5, 6, 9], total=11 → 2

Greedily taking 9 + 1 + 1 uses three bills, while 6 + 5 needs only two.

Constraints
  • 1 ≤ length of denominations ≤ 12
  • 1 ≤ denominations[i] ≤ 2³¹ - 1
  • 0 ≤ total ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSteps to the Nearest Zero MediumFarthest From Any Shore MediumGrasshopper Going Home MediumBlock the Only Route MediumStones on a 3 × 3 Board Medium
loading Python…
Input
denominations=[4, 3, 10], total=13
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 121 tests on the server, hidden ones included.