We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6a7579e commit 16b513cCopy full SHA for 16b513c
1 file changed
โcoin-change/ICE0208.javaโ
@@ -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