MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / coinChangeBottomUp1

Function coinChangeBottomUp1

coin_change_322/solution.go:42–69  ·  view source on GitHub ↗

Version 2: Bottom-up approach using MaxInt32 for min comparison

(coins []int, amount int)

Source from the content-addressed store, hash-verified

40
41// Version 2: Bottom-up approach using MaxInt32 for min comparison
42func coinChangeBottomUp1(coins []int, amount int) int {
43 if amount == 0 {
44 return 0
45 }
46
47 mins := make([]int, amount+1)
48 mins[0] = 0
49
50 for a := 1; a <= amount; a++ {
51 min := math.MaxInt32
52 for c := 0; c < len(coins); c++ {
53 // coin is larger than current amount
54 if coins[c] > a {
55 continue
56 }
57
58 min = int(math.Min(float64(min), float64(mins[a-coins[c]])))
59 }
60
61 mins[a] = min + 1
62 }
63
64 if mins[amount] == math.MaxInt32+1 {
65 return -1
66 }
67
68 return mins[amount]
69}
70
71// Version 1: Top-down approach using recursion
72func coinChangeTopDown(coins []int, amount int) int {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected