-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
300d1c1
commit 848dc5d
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Time Complexity: O(coins.length * amount) | ||
Space Complexity: O(amount) | ||
1 ~ i-1원까지의 최적해를 알고 있다면, i원의 최적해를 구할 때 coins 배열 iteration으로 구할 수 있음 | ||
*/ | ||
class Solution { | ||
public int coinChange(int[] coins, int amount) { | ||
int c = coins.length; | ||
|
||
int[] dp = new int[amount + 1]; | ||
Arrays.fill(dp, 99999); | ||
dp[0] = 0; | ||
|
||
for (int i = 1; i <= amount; i++) { | ||
for (int j = 0; j < c; j++) { | ||
if (i - coins[j] < 0) { | ||
continue; | ||
} | ||
if (dp[i - coins[j]] >= 0 && dp[i - coins[j]] + 1 < dp[i]) { | ||
dp[i] = dp[i - coins[j]] + 1; | ||
} | ||
} | ||
} | ||
|
||
return dp[amount] == 99999 ? -1 : dp[amount]; | ||
} | ||
} |