-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin-change.go
46 lines (39 loc) · 1.31 KB
/
coin-change.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
func coinChange(coins []int, amount int) int {
// initialize an array where the index represents the amount up to target amount
// and the value represents the minimum number of coins needed for each amount
dp := make([]int, amount+1)
for i := 1; i < amount+1; i++ {
// initialize the current table value with amount + 1
// this value will be updated with the minimum number of coins needed
dp[i] = amount + 1
for _, coin := range coins {
if coin <= i {
// for each coin that less or equal than current value
// we calculate number of coins needed by
// looking a previous minimum number of coins for
// [current amount - current coin] + 1 (current coin)
// and take minimum number of coins needed for that amount
dp[i] = min(dp[i], dp[i-coin]+1)
}
}
}
// if the initial value for the requested amount not updated
// it indicates that there is no solution
if dp[amount] == amount+1 {
return -1
}
return dp[amount]
}
// Time: O(n)
// Space: O(n)
// Where n is the target amount + 1, and since it's maximum 12 coins, we consider it as a constant
// Helper function to find minimum between two integers
// Another option is to use math.Min()
// but it's more verbose and require conversion to and from float64
func min(a, b int) int {
if a < b {
return a
}
return b
}