Ctrl K

Ways to Pay an Exact Amount

Medium
A post office sells stamps in the values listed in stamps, with an unlimited supply of each value. You need to put stamps worth exactly total on an envelope. Count how many different sets of stamps do that. The order of sticking them on does not matter: two sets differ only in how many stamps of each value they use. If the amount cannot be made, return 0. The answer is guaranteed to fit in a signed 32-bit integer.
Example 1
total=6, stamps=[1, 3, 4] → 4

The sets 1+1+1+1+1+1, 3+1+1+1, 3+3 and 4+1+1 work, 4 in total.

Example 2
total=7, stamps=[2, 4] → 0

Even values can never add up to an odd total.

Example 3
total=0, stamps=[5] → 1

There is exactly one way to pay zero: use no stamps.

Constraints
  • 1 ≤ len(stamps) ≤ 300
  • 1 ≤ stamps[i] ≤ 5000
  • All values are distinct
  • 0 ≤ total ≤ 5000
  • The answer is below 2³¹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
total=6, stamps=[1, 3, 4]
Expected
4

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