Skip to content

Commit 42e4dbb

Browse files
committed
322. Coin Change
1 parent 7912a57 commit 42e4dbb

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

solution/0322.Coin Change/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 322. Coin Change
2+
3+
You are given coins of different denominations and a total amount of money amount. Write a function to compute 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.
4+
5+
## Example 1:
6+
```
7+
Input: coins = [1, 2, 5], amount = 11
8+
Output: 3
9+
Explanation: 11 = 5 + 5 + 1
10+
```
11+
12+
## Example 2:
13+
```
14+
Input: coins = [2], amount = 3
15+
Output: -1
16+
```
17+
18+
## Note:
19+
You may assume that you have an infinite number of each kind of coin.

solution/0322.Coin Change/Solution.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const coinChange = function(coins, amount) {
2+
let dp = Array(amount + 1).fill(amount + 1);
3+
dp[0] = 0;
4+
for (let i = 1; i <= amount; i++) {
5+
for (let j = 0; j < coins.length; j++) {
6+
if (coins[j] <= i) {
7+
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
8+
}
9+
}
10+
}
11+
12+
return dp[amount] > amount ? -1 : dp[amount];
13+
};

solution/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@
867867
│   └── Solution.cpp
868868
├── 0319.Bulb Switcher
869869
│   └── Solution.java
870+
├── 0322.Coin Change
871+
│   └── Solution.js
870872
├── 0326.Power of Three
871873
│   └── Solution.js
872874
├── 0328.Odd Even Linked List

0 commit comments

Comments
 (0)