TIC
The Interns Company

Coin Change

MediumAcceptance: 40.2%

Problem Statement

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Constraints:

  • 1 <= coins.length <= 12
  • 1 <= coins[i] <= 2^31 - 1
  • 0 <= amount <= 10^4

Input Format:

  • An integer array coins representing the available coin denominations
  • An integer amount representing the total amount of money

Output Format:

  • An integer representing the fewest number of coins needed to make up the amount, or -1 if not possible

Examples:

Example 1:

Input:

coins = [1,2,5], amount = 11

Output:

3

Explanation:

11 = 5 + 5 + 1

Example 2:

Input:

coins = [2], amount = 3

Output:

-1

Explanation:

It is not possible to make the amount 3 with the given coins.

Example 3:

Input:

coins = [1], amount = 0

Output:

0

Explanation:

No coins are needed to make the amount 0.

Example 4:

Input:

coins = [1,3,4,5], amount = 7

Output:

2

Explanation:

7 = 3 + 4

Solutions

Dynamic Programming (Bottom-Up) Approach

Time: O(amount * n) where n is the number of coin denominations
Space: O(amount)

Use a bottom-up dynamic programming approach to build solutions for smaller amounts and work up to the target amount.

BFS (Breadth-First Search) Approach

Time: O(amount * n) where n is the number of coin denominations
Space: O(amount)

Treat the problem as finding the shortest path from 0 to the amount, where each step represents using a coin.

Dynamic Programming with Memoization (Top-Down) Approach

Time: O(amount * n) where n is the number of coin denominations
Space: O(amount)

Use recursion with memoization to solve the problem from the target amount down to smaller amounts.

Algorithm Walkthrough

Example input: nums = []

Step-by-step execution:

  1. Bottom-Up Dynamic Programming Approach:
  2. Initialize dp array: [0, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞]
  3. Process coin = 1:
  4. dp[1] = min(∞, 1 + dp[0]) = min(∞, 1 + 0) = 1
  5. dp[2] = min(∞, 1 + dp[1]) = min(∞, 1 + 1) = 2
  6. ...
  7. dp[11] = min(∞, 1 + dp[10]) = min(∞, 1 + 10) = 11
  8. dp = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  9. Process coin = 2:
  10. dp[2] = min(2, 1 + dp[0]) = min(2, 1 + 0) = 1
  11. dp[3] = min(3, 1 + dp[1]) = min(3, 1 + 1) = 2
  12. ...
  13. dp[11] = min(11, 1 + dp[9]) = min(11, 1 + 5) = 6
  14. dp = [0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6]
  15. Process coin = 5:
  16. dp[5] = min(3, 1 + dp[0]) = min(3, 1 + 0) = 1
  17. dp[6] = min(3, 1 + dp[1]) = min(3, 1 + 1) = 2
  18. ...
  19. dp[11] = min(6, 1 + dp[6]) = min(6, 1 + 2) = 3
  20. dp = [0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 3]
  21. Final result: dp[11] = 3, which means we need 3 coins to make amount 11

Hints

Hint 1
Think about this problem in terms of finding the shortest path to the amount from 0.
Hint 2
Consider using dynamic programming to build up solutions for smaller amounts.
Hint 3
For each amount from 1 to the target, determine the minimum number of coins needed.
Hint 4
For each coin, check if using it results in a smaller number of coins for the current amount.
Hint 5
You can also approach this as a BFS problem, where each level represents the number of coins used.

Video Tutorial

Video tutorials can be a great way to understand algorithms visually

Visualization

Visualize the dynamic programming table as it is filled, showing how the minimum number of coins is calculated for each amount.

Key visualization elements:

  • current coin
  • current amount
  • dp table updates

Implementation Notes

This is a classic dynamic programming problem that can be approached in multiple ways. The key insight is to build up solutions for smaller amounts and reuse them to solve the larger problem. The bottom-up approach is generally more efficient as it avoids the overhead of recursion.