g0301_0400.s0322_coin_change.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
package g0301_0400.s0322_coin_change;
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Breadth_First_Search
// #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_20
// #Level_2_Day_12_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(amount)
// #2024_11_16_Time_12_ms_(92.59%)_Space_44.3_MB_(64.02%)
public class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
dp[0] = 1;
for (int coin : coins) {
for (int i = coin; i <= amount; i++) {
int prev = dp[i - coin];
if (prev > 0) {
if (dp[i] == 0) {
dp[i] = prev + 1;
} else {
dp[i] = Math.min(dp[i], prev + 1);
}
}
}
}
return dp[amount] - 1;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy