#3592

Inverse Coin Change

Medium
ArrayDynamic ProgrammingHash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

We can identify the smallest denomination by finding the first index where numWays[i] == 1. This denomination must be included, and we can derive others from it.

⚙️

Algorithm

3 steps
  1. 1Step 1: Initialize an empty list for denominations.
  2. 2Step 2: Find the smallest index c where numWays[c] == 1 and add c to denominations.
  3. 3Step 3: For each subsequent amount, check if numWays[i] can be formed using previously found denominations.
solution.py6 lines
1def recoverDenominations(numWays):
2    denominations = []
3    for i in range(1, len(numWays)):
4        if numWays[i] == 1:
5            denominations.append(i)
6    return sorted(denominations)

Complexity note: This complexity is efficient as we only traverse the numWays array once to find valid denominations.

  • 1The smallest denomination must have exactly one way to form it.
  • 2Denominations can be derived from the structure of the numWays array.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.