Skip to content

Commit d35c00d

Browse files
authored
Create yeonguchoe.java
1 parent 4bf41fd commit d35c00d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
private Integer[] memo;
3+
4+
public int helper(int[] coins, int remain) {
5+
if (remain < 0) { // 0 ์ดํ•˜๋กœ ๊ฐ€๋Š” ๊ฒฝ์šฐ -1๋กœ ์ง€์ •
6+
return -1;
7+
}
8+
if (remain == 0) { // ์ฒ˜์Œ ๊ฐ’์€ 0์œผ๋กœ ์„ค์ •
9+
return 0;
10+
}
11+
12+
if (memo[remain] != null) {
13+
return memo[remain];
14+
}
15+
16+
int minNumCoin = Integer.MAX_VALUE; // ์„ธ๊ฐœ ์ค‘์— ์ตœ์†Œ๊ฐ’์„ ๊ณ ๋ฅด๋Š” ๋กœ์ง
17+
18+
for (int coin : coins) {
19+
int numCoin = helper(coins, remain - coin);
20+
if (numCoin != -1) { // ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
21+
minNumCoin = Math.min(minNumCoin, numCoin + 1);
22+
}
23+
}
24+
if (minNumCoin < Integer.MAX_VALUE) { // ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
25+
memo[remain] = minNumCoin;
26+
} else {
27+
memo[remain] = -1;
28+
}
29+
30+
return memo[remain];
31+
}
32+
33+
public int coinChange(int[] coins, int amount) {
34+
memo = new Integer[amount + 1];
35+
return helper(coins, amount);
36+
}
37+
}

0 commit comments

Comments
ย (0)