Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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]. |
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 * 108Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[0,1,0,2,0,3,0,4,0,5]
[1,2,2,3,4]
[1,2,3,4,15]
coin-change)coin-change-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3592: Inverse Coin Change
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3592: Inverse Coin Change
// package main
//
// // https://space.bilibili.com/206214
// func findCoins(numWays []int) (ans []int) {
// n := len(numWays)
// f := make([]int, n+1)
// f[0] = 1
// for i := 1; i <= n; i++ {
// ways := numWays[i-1]
// if ways == f[i] {
// continue
// }
// if ways-1 != f[i] {
// return nil
// }
// ans = append(ans, i)
// // 现在得到了一个大小为 i 的物品,用 i 计算完全背包
// for j := i; j <= n; j++ {
// f[j] += f[j-i]
// }
// }
// return
// }
// Accepted solution for LeetCode #3592: Inverse Coin Change
package main
// https://space.bilibili.com/206214
func findCoins(numWays []int) (ans []int) {
n := len(numWays)
f := make([]int, n+1)
f[0] = 1
for i := 1; i <= n; i++ {
ways := numWays[i-1]
if ways == f[i] {
continue
}
if ways-1 != f[i] {
return nil
}
ans = append(ans, i)
// 现在得到了一个大小为 i 的物品,用 i 计算完全背包
for j := i; j <= n; j++ {
f[j] += f[j-i]
}
}
return
}
# Accepted solution for LeetCode #3592: Inverse Coin Change
# Time: O(n^2)
# Space: O(1)
# dp
class Solution(object):
def findCoins(self, numWays):
"""
:type numWays: List[int]
:rtype: List[int]
"""
result = []
for i in xrange(1, len(numWays)+1):
if numWays[i-1] == 1:
result.append(i)
for j in reversed(xrange(i, len(numWays)+1)):
numWays[j-1] -= numWays[(j-i)-1] if (j-i)-1 >= 0 else 1
if numWays[i-1]:
return []
return result
# Time: O(n^2)
# Space: O(n)
# dp
class Solution2(object):
def findCoins(self, numWays):
"""
:type numWays: List[int]
:rtype: List[int]
"""
result = []
dp = [0]*(len(numWays)+1)
dp[0] = 1
for i in xrange(1, len(numWays)+1):
if numWays[i-1]-dp[i] == 1:
result.append(i)
for j in xrange(i, len(numWays)+1):
dp[j] += dp[j-i]
if numWays[i-1]-dp[i]:
return []
return result
// Accepted solution for LeetCode #3592: Inverse Coin Change
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3592: Inverse Coin Change
// package main
//
// // https://space.bilibili.com/206214
// func findCoins(numWays []int) (ans []int) {
// n := len(numWays)
// f := make([]int, n+1)
// f[0] = 1
// for i := 1; i <= n; i++ {
// ways := numWays[i-1]
// if ways == f[i] {
// continue
// }
// if ways-1 != f[i] {
// return nil
// }
// ans = append(ans, i)
// // 现在得到了一个大小为 i 的物品,用 i 计算完全背包
// for j := i; j <= n; j++ {
// f[j] += f[j-i]
// }
// }
// return
// }
// Accepted solution for LeetCode #3592: Inverse Coin Change
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3592: Inverse Coin Change
// package main
//
// // https://space.bilibili.com/206214
// func findCoins(numWays []int) (ans []int) {
// n := len(numWays)
// f := make([]int, n+1)
// f[0] = 1
// for i := 1; i <= n; i++ {
// ways := numWays[i-1]
// if ways == f[i] {
// continue
// }
// if ways-1 != f[i] {
// return nil
// }
// ans = append(ans, i)
// // 现在得到了一个大小为 i 的物品,用 i 计算完全背包
// for j := i; j <= n; j++ {
// f[j] += f[j-i]
// }
// }
// return
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.