Minimal Coin Exchange with Overpayment
Dynamic ProgrammingAuto-graded · 24 test casesLast updated
Find the minimum combined count of paid and received coins to settle a precise target value using dynamic programming.
You are purchasing an item of cost target using a set of available coin values given in coins.
When paying, you may overpay by tendering a total amount $P \ge \text{target}$. The vendor will then return change of amount $P - \text{target}$. Both the payment $P$ and the change $P - \text{target}$ must be represented using valid combinations of the coin values provided in coins.
Your goal is to find the minimum total count of coins exchanged during the transaction (the sum of coins you hand over plus the coins returned to you by the vendor).
If it is impossible to pay and receive change such that the net transaction equals target, return -1.
Assumptions & Rules
- You may assume that the optimal overpayment does not require change exceeding
target + max(coins). - Each coin denomination can be used an unlimited number of times for both paying and making change.
Constraints
1 <= target <= 10,0001 <= len(coins) <= 201 <= coins[i] <= 1,000
Examples
Example 1
Input: min_total_coins([1, 5, 10], 3)
Output: 3
Example 2
Input: min_total_coins([2, 5], 3)
Output: 2
Hints
Hint 1
Consider fixing the total payment amount P >= target. Once P is fixed, the payment (P) and change (P - target) become independent coin change problems.
Hint 2
You can compute a single DP array up to 2 * target + max(coins) that stores the minimum coins needed for any amount.
Hint 3
Iterate over all plausible payment amounts P from target to 2 * target + max(coins), and minimize dp[P] + dp[P - target].
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Method Explanation
The key insight is that once a payment amount $P$ (where $P \ge \text{target}$) is selected, the payment phase and change phase become completely independent:
- You pay $P$ using the fewest possible coins from
coins. - The merchant returns $P - \text{target}$ using the fewest possible coins from
coins.
Both operations are instances of the standard Unbounded Coin Change problem. Therefore, a single Dynamic Programming (DP) table dp[a]—representing the minimum coins needed to form an exact value a—can be shared across both payment and change.
- Upper Bound: The change required will not exceed $\text{max_overpay} = \text{target} + \max(\text{coins})$. Thus, the maximum payment we need to evaluate is $\text{limit} = 2 \times \text{target} + \max(\text{coins})$.
- DP Construction: We compute
dp[a]for all $0 \le a \le \text{limit}$. - Minimization: We iterate over all valid payment values $P \in [\text{target}, \text{limit}]$ and find the minimum value of
dp[P] + dp[P - target].
Complexity Analysis
- Time Complexity: $\mathcal{O}(N \cdot L)$ where $N = \text{len(coins)}$ and $L = 2 \times \text{target} + \max(\text{coins})$. Given the constraints ($N \le 20, L \le 21,000$), the DP table requires fewer than $5 \times 10^5$ operations, which runs well within time limits.
- Space Complexity: $\mathcal{O}(L)$ to store the
dparray of size up to $21,001$.
Python Source Code
from typing import List
def min_total_coins(coins: List[int], target: int) -> int:
max_overpay = target + max(coins)
limit = target + max_overpay
inf = 10**9
# dp[a] stores the minimum number of coins needed to make amount 'a'
dp = [inf] * (limit + 1)
dp[0] = 0
for amount in range(1, limit + 1):
best = inf
for coin in coins:
if coin <= amount and dp[amount - coin] != inf:
best = min(best, dp[amount - coin] + 1)
dp[amount] = best
answer = inf
for payment in range(target, limit + 1):
change = payment - target
if dp[payment] != inf and dp[change] != inf:
answer = min(answer, dp[payment] + dp[change])
return -1 if answer == inf else answer