[LeetCode] Inverse Coin Change

3592. Inverse Coin Change

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

class Solution {
public:
vector<int> findCoins(vector<int>& numWays) {
vector<int> res;
vector<int> dp(numWays.size() + 1);
dp[0] = 1;
for(int i = 0; i < numWays.size(); i++) {
if(numWays[i] == dp[i+1]) continue;
if(numWays[i] - dp[i+1] == 1) {
res.push_back(i+1);
for(int j = i + 1; j < dp.size(); j++) dp[j] += dp[j-(i+1)];
} else return {};
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/06/23/PS/LeetCode/inverse-coin-change/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.