Use a Boolean dynamic programming state for whether amount can be made exactly. Initialize and, for , computeAn empty disjunction is false. Any nonempty representation ends in some coin; removing it gives one of the displayed predecessor states, and adding it back proves the converse. Return . This takes time and space for positive integer denominations and nonnegative integer target , with unlimited coins of each type.
For the minimum coin change recurrence, initialize and usewhere the minimum over no feasible predecessor is and . Removing the last coin of an optimal representation proves the lower bound; appending the corresponding coin to an optimal predecessor proves attainability. is the minimum number of coins, obtained in time and space. Store one minimizing coin per amount if the actual coins are wanted.
For , use largest-denomination-first greedy algorithm steps: for , take coins and replace the residual amount by . A target is representable exactly when it is divisible by , because the smallest denomination is , not one.
To prove optimality whenever the target is representable, observe that an optimum can have fewer than coins of every denomination with : replacing such coins by one coin preserves value and reduces the count by . After all such carrying, the coefficients of are the unique base- digits of , while the coefficient of is the remaining quotient. Largest-first division produces exactly these coefficients. The greedy coin count is optimal, and can be computed with quotient/remainder operations without listing every coin.
For , distinct denominations permit only ; then all coins have value one and the answer is . This handles the printed positive-integer condition without silently assuming .
Articles by others on the same topic
There are currently no matching articles.