bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 322. Coin Change

322. Coin Change

Leetcode Solutions

Dynamic Programming - Bottom Up Approach

  1. Initialize an array dp of size amount + 1 and fill it with amount + 1. This represents the minimum number of coins needed for each amount from 0 to amount.
  2. Set dp[0] to 0.
  3. Loop over the range from 1 to amount (inclusive), calling each value i.
  4. For each i, loop over each coin in coins.
  5. If the coin value is less than or equal to i, set dp[i] to the minimum of dp[i] and dp[i - coin] + 1.
  6. After filling the dp table, check if dp[amount] is greater than amount. If so, return -1.
  7. Otherwise, return dp[amount].
UML Thumbnail

Dynamic Programming - Top Down Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR