Skip to content

Commit 69b7a79

Browse files
authored
feat: coin change
1 parent f625aac commit 69b7a79

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

coin-change/minji-go.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Problem: https://leetcode.com/problems/coin-change/
3+
Description: return the fewest number of coins that you need to make up that amount
4+
Concept: Array, Dynamic Programming, Breadth-First Search
5+
Time Complexity: O(N²), Runtime 15ms - N is the amount
6+
Space Complexity: O(N), Memory 44.28MB
7+
*/
8+
class Solution {
9+
public int coinChange(int[] coins, int amount) {
10+
int[] dp = new int[amount+1];
11+
Arrays.fill(dp, amount+1);
12+
dp[0]=0;
13+
14+
for(int i=1; i<=amount; i++){
15+
for(int coin : coins){
16+
if(i >= coin) {
17+
dp[i] = Math.min(dp[i], dp[i-coin] +1);
18+
}
19+
}
20+
}
21+
return dp[amount]>amount? -1: dp[amount];
22+
}
23+
}

0 commit comments

Comments
 (0)