Skip to content

Commit 16b513c

Browse files
committed
coin change
1 parent 6a7579e commit 16b513c

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

โ€Žcoin-change/ICE0208.javaโ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
/**
3+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(amount * coins.length)
4+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(amount)
5+
*/
6+
public int coinChange(int[] coins, int amount) {
7+
// ํ•„์š”ํ•œ ๋™์ „์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” amount๊ฐœ์ด๋ฏ€๋กœ,
8+
// amount + 1์€ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ์— ์ถฉ๋ถ„ํ•˜๋‹ค.
9+
int impossible = amount + 1;
10+
11+
int[] dp = new int[amount + 1];
12+
Arrays.fill(dp, impossible);
13+
dp[0] = 0;
14+
15+
for (int currentAmount = 1; currentAmount <= amount; currentAmount++) {
16+
for (int coin : coins) {
17+
if (coin > currentAmount) {
18+
continue;
19+
}
20+
21+
dp[currentAmount] = Math.min(
22+
dp[currentAmount],
23+
dp[currentAmount - coin] + 1
24+
);
25+
}
26+
}
27+
28+
return dp[amount] == impossible ? -1 : dp[amount];
29+
}
30+
}

0 commit comments

Comments
ย (0)