Inverse Coin Change Problem
Description
LeetCode Problem 3592.
You are given a 1-indexed integer array numWays, where numWays[i] represents the number of ways to select a total amount i using an infinite supply of some fixed coin denominations. Each denomination is a positive integer with value at most numWays.length.
However, the exact coin denominations have been lost. Your task is to recover the set of denominations that could have resulted in the given numWays array.
Return a sorted array containing unique integers which represents this set of denominations.
If no such set exists, return an empty array.
Example 1:
Input: numWays = [0,1,0,2,0,3,0,4,0,5]
Output: [2,4,6]
Explanation:
Amount Number of ways Explanation
1 0 There is no way to select coins with total value 1.
2 1 The only way is [2].
3 0 There is no way to select coins with total value 3.
4 2 The ways are [2, 2] and [4].
5 0 There is no way to select coins with total value 5.
6 3 The ways are [2, 2, 2], [2, 4], and [6].
7 0 There is no way to select coins with total value 7.
8 4 The ways are [2, 2, 2, 2], [2, 2, 4], [2, 6], and [4, 4].
9 0 There is no way to select coins with total value 9.
10 5 The ways are [2, 2, 2, 2, 2], [2, 2, 2, 4], [2, 4, 4], [2, 2, 6], and [4, 6].
Example 2:
Input: numWays = [1,2,2,3,4]
Output: [1,2,5]
Explanation:
Amount Number of ways Explanation
1 1 The only way is [1].
2 2 The ways are [1, 1] and [2].
3 2 The ways are [1, 1, 1] and [1, 2].
4 3 The ways are [1, 1, 1, 1], [1, 1, 2], and [2, 2].
5 4 The ways are [1, 1, 1, 1, 1], [1, 1, 1, 2], [1, 2, 2], and [5].
Example 3:
Input: numWays = [1,2,3,4,15]
Output: []
Explanation:
No set of denomination satisfies this array.
Constraints:
1 <= numWays.length <= 1000 <= numWays[i] <= 2 * 10^8
Solution
Approach: Rebuild the Coins Greedily
Process amounts from 1 to n in increasing order while keeping dp[i], the number of ways to make i with the coins found so far (dp[0] = 1).
At amount i, compare dp[i] with numWays[i]:
- Equal: the coins found so far already explain this amount, so
iis not a coin. numWays[i] - dp[i] == 1: the only way left is the single coini. Addito the answer and updatedpas in unbounded knapsack:dp[j] += dp[j - i]forjfromiton.- Any other difference: a new coin
iadds exactly one way for amounti, and smaller coins are already fixed, so no set of coins fits. Return an empty array.
Complexity: O(n2) time and O(n) extra space.
class Solution:
def findCoins(self, numWays: List[int]) -> List[int]:
n = len(numWays)
dp = [0] * (n + 1)
dp[0] = 1
ans = []
for i in range(1, n + 1):
d = numWays[i - 1] - dp[i]
if d == 0:
continue
if d != 1:
return []
ans.append(i)
for j in range(i, n + 1):
dp[j] += dp[j - i]
return ans