Minimum Coins to Reach a Money Amount
Dynamic ProgrammingAuto-graded · 22 test casesLast updated
Tests dynamic programming on a coin-change problem where decimal monetary values must be safely converted to integers before solving.
You are given a list of coin denominations as decimal numbers and a target amount as a decimal number. Each denomination can be used as many times as needed. Find the fewest number of coins whose total value equals the target exactly. If no combination of coins sums to the target, return -1.
All monetary values use at most two decimal places (e.g., 0.25, 1.50, 3.00). Because floating-point numbers cannot represent most decimal fractions exactly, you must convert all values to integers (cents) before performing any computation. Do not run dynamic programming directly on floats.
Constraints:
1 <= len(denominations) <= 1000 < each denomination <= 10,0000 <= target <= 10,000- All values have at most two digits after the decimal point
- Aim for
O(target * number_of_denominations)time after scaling andO(target)space
Examples
Example 1
Input: min_coins_for_amount([0.25, 0.5, 1], 1.25)
Output: 2
Example 2
Input: min_coins_for_amount([0.1, 0.2, 0.5], 0.7)
Output: 2
Example 3
Input: min_coins_for_amount([0.3, 0.5], 0.7)
Output: -1
Hints
Hint 1
Convert each decimal to cents by multiplying by 100, but beware that int(1.15 * 100) gives 114 due to float representation — use Decimal(str(value)) to avoid this.
Hint 2
Build a dp array where dp[v] stores the minimum coins needed to make exactly v cents, with dp[0] = 0 as the base case.
Hint 3
Use a sentinel value larger than any possible answer (like amount + 1) to mark unreachable amounts, then return -1 if dp[amount] is still the sentinel.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem is an unbounded coin-change problem, but the inputs are decimal money values. The key insight is that money with at most two decimal places is really an integer number of cents. So we work in three steps:
-
Scale to integers safely. Convert each decimal value to cents by multiplying by 100. The catch is that floating-point arithmetic introduces tiny errors: for example,
1.15is stored as1.149999999999999911..., soint(1.15 * 100)yields114instead of115. We avoid this by going throughDecimal(str(value))— Python'sstr(float)produces the shortest string that round-trips back to that float (e.g.,"1.15"), andDecimalperforms exact base-10 arithmetic, soDecimal("1.15") * 100is exactly115. Note thatDecimal(1.15)(constructed directly from the float) would not help, since it faithfully captures the binary error. -
Solve the integer coin-change DP. Let
dp[v]be the minimum number of coins that sum to exactlyvcents. The recurrence isdp[0] = 0anddp[v] = min(dp[v - c] + 1)over every coinc <= v. We fillvin increasing order sodp[v - c]is always computed before it is needed. Coins may be reused becausedp[v - c]itself may already usec. -
Map the result back. If
dp[amount]is still the sentinel (initialized toamount + 1, which is larger than any real answer), return-1.
Why It Is Correct
Scaling is exact. Every value has at most two decimal places. str() recovers the decimal literal, and Decimal arithmetic is exact base-10, so Decimal(str(x)) * 100 is an exact integer. Two amounts are equal in dollars if and only if they are equal in cents, so the integer problem has exactly the same solutions as the decimal problem.
The DP is correct. Any optimal way to make v cents has some last coin c. The remaining coins form a way to make v - c, and that remainder must itself be optimal, or we could improve the total. The recurrence tries every possible last coin, so it finds the optimum.
The sentinel is safe. Every scaled coin is at least 1 cent, so any reachable amount needs at most amount coins. amount + 1 can never be a real answer, making it a reliable sentinel.
The early break is valid. Coins are sorted ascending; once coin > v, every later coin is also too large, so we can stop the inner loop.
Greedy fails on arbitrary coin systems. For example, with coins [0.01, 0.06, 0.10] and target 0.12, greedy picks 0.10 + 0.01 + 0.01 (3 coins) but the DP correctly finds 0.06 + 0.06 (2 coins).
Complexity
Let A be the target in cents (up to 1,000,000) and n the number of distinct coins (up to 100).
- Time:
O(n log n)for sorting plusO(A * n)for the DP. - Space:
O(A)for thedparray plusO(n)for the coin set.
Edge Cases
- Target is
0: return0immediately (zero coins make zero). - Target unreachable:
dp[amount]stays at the sentinel, so return-1. - Duplicate denominations: the set comprehension removes duplicates.
- Coins larger than the target: skipped by the early break.
Full Solution
from decimal import Decimal
from typing import List
def min_coins_for_amount(denominations: List[float], target: float) -> int:
scale = 100
# Convert each denomination to integer cents safely
coins = sorted({int(Decimal(str(d)) * scale) for d in denominations})
amount = int(Decimal(str(target)) * scale)
if amount == 0:
return 0
INF = amount + 1
dp = [INF] * (amount + 1)
dp[0] = 0
for v in range(1, amount + 1):
best = INF
for coin in coins:
if coin > v:
break
cand = dp[v - coin] + 1
if cand < best:
best = cand
dp[v] = best
return -1 if dp[amount] == INF else dp[amount]