mediumBacktrackingAmazonFacebookSnapchat
Coin Combo Mint
A vending mint lists every multiset of coin denominations whose face values sum to a target amount.
Given distinct positive integers and a target, return all unique combinations that sum to the target. Each denomination may be chosen unlimited times.
Choose approach
2
0j
3
16
27
3SetCombos (0)
empty
1/19
Narrationstep 1 of 19
DFS from index 0 with target 7.
Variables
remain=7
cur=[]
outCount=0
How to think about it
- Sorting enables the `cands[j] > remain` prune.
- Reusing index `j` instead of `j+1` allows unlimited picks of the same denomination.
- Hitting remain=0 emits a combo; negative remain is pruned by the break.
Edge cases to catch in interview
- Target 0 -> single empty combo.
- No candidate ≤ target -> empty.
Examples
Example 1
[[2,2,3],[7]]{"values":[2,3,6,7],"target":7}
Example 2
[[2,2,2,2],[2,3,3],[3,5]]{"values":[2,3,5],"target":8}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed