Payments app~30 min2s · 256 MBtarget O(k × total) time, O(total) spacedp
You work on a payments terminal that pays refunds in cash. The drawer holds notes of several kinds. Each kind has a face value in units and a limited number of notes.
A refund must be paid with exactly the right amount, and a note cannot be split. Two payouts are the same when they use the same number of notes of every kind. The order in which the notes are handed over does not matter.
Your team lead asks: "In how many different ways can this refund be paid?" The drawer has at most 40 kinds, and the refund is at most 20,000 units.
Write count_payouts(kinds, total).
kinds[i] = [value, count] is a kind of note: its face value in units, and how many notes of it the drawer holds.count. The notes must add up to exactly total.1,000,000,007. A total of 0 has one payout, which uses no notes.Input: kinds = [[1, 2], [2, 2], [5, 1]], total = 5
Output: 2The drawer holds two notes of 1, two notes of 2 and one note of 5. Every way to reach 5, as a number of notes of each kind:
| Notes of 1 | Notes of 2 | Notes of 5 | Sum |
|---|---|---|---|
| 0 | 0 | 1 | 0×1 + 0×2 + 1×5 = 5 |
| 1 | 2 | 0 | 1×1 + 2×2 + 0×5 = 5 |
No other mix adds up to 5 within the stock.
Input: kinds = [[3, 2]], total = 0
Output: 1Paying nothing is one way: use no notes.
Input: kinds = [[2, 1]], total = 4
Output: 0One note of 2 adds up to 2, so 4 cannot be reached.
1 ≤ len(kinds) ≤ 401 ≤ kinds[i][0], kinds[i][1] ≤ 1,000,0000 ≤ total ≤ 20,0001,000,000,007.Choosing a number of notes for every kind multiplies the choices. With 40 kinds of 50 or more notes each, there are more than 50 to the power 40 mixes. A table with one entry for each amount from 0 to 20,000 is tiny. The work is one pass over the table for each kind, which is 800,000 steps.