Skip to content

Commit d4d3975

Browse files
committed
feat : leetCode dp coin-change 풀이
1 parent bb4f3ce commit d4d3975

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/main/java/sgyj/leetcode/Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@
3030

3131
## DP
3232

33-
- [746. Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/)
33+
- [746. Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/)
34+
- [128. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)
35+
- [322. Coin Change](https://leetcode.com/problems/coin-change/description/)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package sgyj.leetcode.yeji.section6;
2+
3+
import java.util.Arrays;
4+
5+
// coin change
6+
public class Solution322 {
7+
8+
public int coinChange(int[] coins, int amount) {
9+
int[] result = new int[amount+1];
10+
Arrays.fill( result, Integer.MAX_VALUE - 1);
11+
result[0] = 0;
12+
13+
for(int coin : coins){
14+
for(int n = coin; n<=amount; n++){
15+
int tmp = result[n-coin] + 1;
16+
if(result[n]>tmp) result[n] = tmp;
17+
}
18+
}
19+
20+
return result[result.length-1] == Integer.MAX_VALUE-1 ? -1 : result[result.length-1];
21+
}
22+
23+
}

0 commit comments

Comments
 (0)