-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to leetcode problem: No.0322. Coin Change
- Loading branch information
1 parent
09a0eb6
commit efd1993
Showing
4 changed files
with
67 additions
and
24 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
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
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 |
---|---|---|
@@ -1,16 +1,13 @@ | ||
class Solution { | ||
public int coinChange(int[] coins, int amount) { | ||
int n = coins.length; | ||
int[] dp = new int[amount + 1]; | ||
Arrays.fill(dp, amount + 1); | ||
dp[0] = 0; | ||
for (int i = 1; i <= amount; i++) { | ||
for (int j = 0; j < n; j++) { | ||
if (i >= coins[j]) { | ||
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1); | ||
} | ||
} | ||
} | ||
return dp[amount] > amount ? -1 : dp[amount]; | ||
} | ||
} | ||
public int coinChange(int[] coins, int amount) { | ||
int[] dp = new int[amount + 1]; | ||
Arrays.fill(dp, amount + 1); | ||
dp[0] = 0; | ||
for (int coin : coins) { | ||
for (int j = coin; j <= amount; j++) { | ||
dp[j] = Math.min(dp[j], dp[j - coin] + 1); | ||
} | ||
} | ||
return dp[amount] > amount ? -1 : dp[amount]; | ||
} | ||
} |
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,8 @@ | ||
class Solution: | ||
def coinChange(self, coins: List[int], amount: int) -> int: | ||
dp = [amount + 1 for i in range(amount + 1)] | ||
dp[0] = 0 | ||
for coin in coins: | ||
for j in range(coin, amount + 1): | ||
dp[j] = min(dp[j], dp[j - coin] + 1) | ||
return -1 if dp[amount] > amount else dp[amount] |