
g0301_0400.s0322_coin_change.solution.go Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
package s0322_coin_change
// #Medium #Top_100_Liked_Questions #Top_Interview_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_03_16_Time_10_ms_(73.33%)_Space_6.4_MB_(71.07%)
func coinChange(coins []int, amount int) int {
dp := make([]int, amount+1)
for i := range dp {
dp[i] = amount + 1
}
dp[0] = 0
for i := 1; i <= amount; i++ {
for _, c := range coins {
if i < c {
continue
}
dp[i] = min(dp[i], dp[i-c]+1)
}
}
if dp[amount] == amount+1 {
return -1
} else {
return dp[amount]
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy