LeetCode #3592 — MEDIUM

Inverse Coin Change

Move from brute-force thinking to an efficient approach using array strategy.

Solve on LeetCode
The Problem

Problem Statement

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 <= 100
  • 0 <= numWays[i] <= 2 * 108
Patterns Used

Roadmap

  1. Brute Force Baseline
  2. Core Insight
  3. Algorithm Walkthrough
  4. Edge Cases
  5. Full Annotated Code
  6. Interactive Study Demo
  7. Complexity Analysis
Step 01

Brute Force Baseline

Problem 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.

Baseline thinking

Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.

Pattern signal: Array · Dynamic Programming

Example 1

[0,1,0,2,0,3,0,4,0,5]

Example 2

[1,2,2,3,4]

Example 3

[1,2,3,4,15]

Related Problems

  • Coin Change (coin-change)
  • Coin Change II (coin-change-ii)
Step 02

Core Insight

What unlocks the optimal approach

  • Observe that for the smallest denomination <code>c</code>, you must have <code>numWays[c] == 1</code>.
  • Find the smallest <code>c > 0</code> with <code>numWays[c] == 1</code> and append <code>c</code> to your <code>ans</code> list.
  • "Remove" that coin’s contribution by doing, for each <code>s</code> from <code>c</code> up to <code>n</code>: numWays[s] -= numWays[s - c]
  • Repeat: pick the next smallest <code>c</code> with <code>numWays[c] == 1</code>, remove it, and so on.
  • At the end, if <code>numWays</code> is all zeros, your <code>ans</code> is valid; otherwise, return an empty array.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03

Algorithm Walkthrough

Iteration Checklist

  1. Define state (indices, window, stack, map, DP cell, or recursion frame).
  2. Apply one transition step and update the invariant.
  3. Record answer candidate when condition is met.
  4. Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04

Edge Cases

Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Upper-end input sizes
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05

Full Annotated Code

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
// }
Step 06

Interactive Study Demo

Use this to step through a reusable interview workflow for this problem.

Press Step or Run All to begin.
Step 07

Complexity Analysis

Time
O(n × m)
Space
O(n × m)

Approach Breakdown

RECURSIVE
O(2ⁿ) time
O(n) space

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.

DYNAMIC PROGRAMMING
O(n × m) time
O(n × m) space

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.

Shortcut: Count your DP state dimensions → that’s your time. Can you drop one? That’s your space optimization.
Coach Notes

Common Mistakes

Review these before coding to avoid predictable interview regressions.

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.

State misses one required dimension

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.