Minimum Coins with Reusable or Single-Use Entries
Problem statement
The report names coin change with one-dimensional space compression, followed by a change to 0/1 knapsack. It does not reproduce whether the original objective was minimum coins or number of ways.
For this exercise, assume the goal is to return the minimum number of coins with total value exactly amount. The positive entries of coins have two possible meanings:
mode = 0: each entry is a denomination that may be reused any number of times.mode = 1: each array position is one physical coin that can be used at most once. Equal-valued positions are separate coins.
For this exercise, assume a finite mode parameter exposes both versions. Return -1 if the amount cannot be formed and 0 for amount zero. The order of selected coins does not matter. Do not change the input.
Function
minimumCoins(coins: int[], amount: int, mode: int) → intExamples
Example 1
coins = [1,4,6]amount = 8mode = 0return = 2Reuse denomination 4 twice to make 8 with two coins. No listed denomination is 8, so one coin cannot suffice.
Example 2
coins = [1,4,6]amount = 8mode = 1return = -1The possible subset sums are 0,1,4,5,6,7,10 and 11. None is 8, and the entry 4 cannot be used twice.
Example 3
coins = []amount = 0mode = 1return = 0Selecting no coins forms amount zero, even when the input has no entries.
Constraints
- For this exercise, assume
0 <= coins.length <= 100. - For this exercise, assume
1 <= coins[i] <= 2000. - For this exercise, assume
0 <= amount <= 2000. 0 <= mode <= 1.