Overview
This is a classic unbounded knapsack problem. We want to fill a knapsack of capacity `amount` using minimum items of given weights `coins`.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Find the minimum number of coins needed to make up a given amount. You can use each coin infinitely many times.
This is a classic unbounded knapsack problem. We want to fill a knapsack of capacity `amount` using minimum items of given weights `coins`.
Let `dp[i]` be the minimum number of coins needed to make amount `i`. Initially, `dp[0] = 0` and all other `dp[i] = infinity`.
For each coin, we can try to add it to any reachable state `i - coin`. The new state is `dp[i] = Math.min(dp[i], dp[i - coin] + 1)`.
By iterating through each coin sequentially and updating the DP table left-to-right, we implicitly allow the same coin to be used multiple times.