Skip to content

Commit 2af5961

Browse files
committed
add coin change solution
1 parent cb2cf30 commit 2af5961

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

โ€Žcoin-change/Tessa1217.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* ์ •์ˆ˜ ๋ฐฐ์—ด coins๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ amount๋ฅผ ๋งŒ๋“ค ๊ธฐ ์œ„ํ•ด ์ตœ์†Œํ•œ์˜ ๋™์ „ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.
3+
๋งŒ์•ฝ ๋™์ ์œผ๋กœ amount ์กฐํ•ฉ์„ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ๋ฆฌํ„ดํ•˜์„ธ์š”.
4+
*/
5+
import java.util.Arrays;
6+
7+
class Solution {
8+
9+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n * amount), ๊ณต๊ฐ„๋ณต์žก๋„: O(amount)
10+
public int coinChange(int[] coins, int amount) {
11+
int[] coinCnt = new int[amount + 1];
12+
// coins[i]์˜ ์ตœ๋Œ“๊ฐ’์ด 2^31 - 1 ์ด๋ฏ€๋กœ ์ตœ๋Œ“๊ฐ’ ์„ค์ •
13+
Arrays.fill(coinCnt, Integer.MAX_VALUE - 1);
14+
coinCnt[0] = 0;
15+
for (int i = 0; i < coins.length; i++) {
16+
for (int j = coins[i]; j < amount + 1; j++) {
17+
coinCnt[j] = Math.min(coinCnt[j], coinCnt[j - coins[i]] + 1);
18+
}
19+
}
20+
if (coinCnt[amount] == Integer.MAX_VALUE - 1) {
21+
return -1;
22+
}
23+
return coinCnt[amount];
24+
}
25+
}
26+

0 commit comments

Comments
ย (0)