Skip to content

Commit 4b5e1ea

Browse files
authored
Create NumCoinChange.java
1 parent d46675d commit 4b5e1ea

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

NumCoinChange.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
3+
public int coinChange(int[] coins, int amount) {
4+
5+
int[] memo = new int[amount + 1];
6+
7+
Arrays.fill(memo, amount + 1);
8+
9+
memo[0] = 0;
10+
11+
for(int i = 1; i <= amount; i++) {
12+
for(int coin: coins) {
13+
if(coin <= i) {
14+
memo[i] = Math.min(memo[i], memo[i - coin] + 1);
15+
}
16+
}
17+
}
18+
19+
return memo[amount] > amount ? -1 : memo[amount];
20+
21+
}
22+
23+
}

0 commit comments

Comments
 (0)