|
| 1 | +# Coin Change |
| 2 | + |
| 3 | +Page on leetcode: https://leetcode.com/problems/coin-change/ |
| 4 | + |
| 5 | +## Problem Statement |
| 6 | + |
| 7 | +You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +You may assume that you have an infinite number of each kind of coin. |
| 12 | + |
| 13 | +### Constraints |
| 14 | + |
| 15 | +- 1 <= coins.length <= 12 |
| 16 | +- 1 <= coins[i] <= 231 - 1 |
| 17 | +- 0 <= amount <= 104 |
| 18 | + |
| 19 | +### Example |
| 20 | + |
| 21 | +``` |
| 22 | +Input: coins = [1,2,5], amount = 11 |
| 23 | +Output: 3 |
| 24 | +Explanation: 11 = 5 + 5 + 1 |
| 25 | +``` |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +- Greedy algorithm |
| 30 | +- Is the coin array sorted? No |
| 31 | + |
| 32 | +### Pseudocode |
| 33 | + |
| 34 | +1. Sort coins largest to smallest |
| 35 | +2. Create counter variable |
| 36 | +3. Greedy take largest coin until not divisible into remaining amount |
| 37 | +4. Move to next amount and repeat until amount is reached |
| 38 | +5. If at end of coins and can't reach amount return -1 |
| 39 | + |
| 40 | +### Initial Attempt |
| 41 | + |
| 42 | +Greedy doesn't work as it takes a large coin and may return -1 even when you could find a solution with the second largest coin. |
| 43 | + |
| 44 | +```javascript |
| 45 | +const coinChange = function (coins, amount) { |
| 46 | + coins.sort((a, b) => b - a); |
| 47 | + let count = 0; |
| 48 | + for (let i = 0; i < coins.length; i++) { |
| 49 | + if (amount === 0) { |
| 50 | + return count; |
| 51 | + } |
| 52 | + const numDivisible = Math.floor(amount / coins[i]); |
| 53 | + amount -= numDivisible * coins[i]; |
| 54 | + count += numDivisible; |
| 55 | + } |
| 56 | + if (amount > 0) { |
| 57 | + return -1; |
| 58 | + } |
| 59 | + return count; |
| 60 | +}; |
| 61 | +``` |
| 62 | + |
| 63 | +### Optimized Solution |
| 64 | + |
| 65 | +This problem can be solved using bottom up dynamic programming. Time complexity for this solution is O(n\*m) and space complexity is O(n), where n is amount and m is coins length. You can see an explanation of the solution here: https://www.youtube.com/watch?v=H9bfqozjoqs |
| 66 | + |
| 67 | +```javascript |
| 68 | +// Dynamic Programming |
| 69 | +const coinChange = function (coins, amount) { |
| 70 | + // Create array and fill with a max value. This will help later for determining if a solution was found. |
| 71 | + const dpCounts = Array(amount + 1).fill(amount + 1); |
| 72 | + // Base case |
| 73 | + dpCounts[0] = 0; |
| 74 | + |
| 75 | + // Fill out all cache slots with the minimum amount of coins |
| 76 | + for (let i = 1; i < dpCounts.length; i++) { |
| 77 | + for (const coin of coins) { |
| 78 | + // Make sure that coin can actual make up amount i |
| 79 | + if (i - coin >= 0) { |
| 80 | + dpCounts[i] = Math.min(dpCounts[i], 1 + dpCounts[i - coin]); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + // Make sure a solution was found |
| 85 | + if (dpCounts[amount] < amount + 1) { |
| 86 | + return dpCounts[amount]; |
| 87 | + } else { |
| 88 | + return -1; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +// Breadth First Search |
| 93 | +const coinChange = function (coins, amount) { |
| 94 | + // If amount is zero, always returns 0 |
| 95 | + if (amount === 0) { |
| 96 | + return 0; |
| 97 | + } |
| 98 | + // Create array and fill with a temp value. This will help later for determining if a solution was found. |
| 99 | + const counts = Array(amount + 1).fill(-1); |
| 100 | + |
| 101 | + // Create queue for BFS and numOfCoins to track the "depth" of the search |
| 102 | + const queue = [0]; |
| 103 | + let numOfCoins = 0; |
| 104 | + |
| 105 | + while (queue.length > 0) { |
| 106 | + numOfCoins++; |
| 107 | + // Checking nodes for this level |
| 108 | + let levelNodes = queue.length; |
| 109 | + while (levelNodes > 0) { |
| 110 | + const current = queue.shift(); |
| 111 | + // Check current amount plus each coin |
| 112 | + for (const coin of coins) { |
| 113 | + if (current + coin === amount) { |
| 114 | + // Found a match at this level. Since it is BFS this will be the lowest level possible. |
| 115 | + return numOfCoins; |
| 116 | + } |
| 117 | + // Skip if the total is more than the amount or if it has already been calculated |
| 118 | + if (current + coin < counts.length && counts[current + coin] === -1) { |
| 119 | + // Mark amount as calculated and add to queue to be process with next set of coins |
| 120 | + counts[current + coin] = numOfCoins; |
| 121 | + queue.push(current + coin); |
| 122 | + } |
| 123 | + } |
| 124 | + levelNodes--; |
| 125 | + } |
| 126 | + } |
| 127 | + // If BFS completes and numOFCoins doesn't return then no solution exist |
| 128 | + return -1; |
| 129 | +}; |
| 130 | +``` |
0 commit comments