For nonnegative integer amount , let be the minimum number of coins making amount . Set and compute, in increasing order of ,An empty candidate set has minimum . Here denomination ensures that every nonnegative amount is attainable. Removing one coin from an optimal nonempty solution gives a smaller amount, and adding any allowed last coin gives a feasible solution; these two directions prove the recurrence by induction on .
Return whether . This solves the at-most- decision without needing a separate dimension for . The table for the minimum coin change recurrence takes running time and space complexity. The bound is pseudopolynomial time: the amount, rather than its binary encoding length, appears in it. Amount zero needs zero coins.
During the computation in part i, record an index attaining the minimum at every positive amount. Starting with , print and replace by , continuing until .
Every step decreases the amount and reduces the table entry by exactly one. The printed denominations therefore sum to and their number is exactly , proving optimality. The total running time is , including the output; because denomination is available. The predecessor array adds to the space complexity. This is reconstruction from the dynamic programming certificate, not the largest-denomination greedy algorithm, which need not be optimal for arbitrary denominations.
Let use only the first available coins. Initialize , for , and useomitting the second term when . The alternatives exclude the current coin or use it once; both refer to the previous row, so it cannot be used twice. Induction on proves correctness, and is the required answer, including when no selection works.
The running time is . A single amount array reduces auxiliary space to if each coin updates amounts in descending order, . Then still describes the previous row. Ascending updates would reuse the same coin and solve a different, unlimited-supply problem.
Articles by others on the same topic
There are currently no matching articles.