From cc181c359e1b07218add9bfb35f40716cd924e71 Mon Sep 17 00:00:00 2001 From: "go-interview-practice-bot[bot]" <230190823+go-interview-practice-bot[bot]@users.noreply.github.com> Date: Fri, 31 Oct 2025 22:38:58 +0000 Subject: [PATCH] Add solution for Challenge 22 --- .../submissions/Johrespi/solution-template.go | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 challenge-22/submissions/Johrespi/solution-template.go diff --git a/challenge-22/submissions/Johrespi/solution-template.go b/challenge-22/submissions/Johrespi/solution-template.go new file mode 100644 index 00000000..78cd8345 --- /dev/null +++ b/challenge-22/submissions/Johrespi/solution-template.go @@ -0,0 +1,79 @@ +package main + +import ( + "fmt" +) + +func main() { + // Standard U.S. coin denominations in cents + denominations := []int{1, 5, 10, 25, 50} + + // Test amounts + amounts := []int{87, 42, 99, 33, 7} + + for _, amount := range amounts { + // Find minimum number of coins + minCoins := MinCoins(amount, denominations) + + // Find coin combination + coinCombo := CoinCombination(amount, denominations) + + // Print results + fmt.Printf("Amount: %d cents\n", amount) + fmt.Printf("Minimum coins needed: %d\n", minCoins) + fmt.Printf("Coin combination: %v\n", coinCombo) + fmt.Println("---------------------------") + } +} + +// MinCoins returns the minimum number of coins needed to make the given amount. +// If the amount cannot be made with the given denominations, return -1. +func MinCoins(amount int, denominations []int) int { + // TODO: Implement this function + min := 0 + sum :=0 + + for i := len(denominations) -1 ; i >= 0; i-- { + for { + if sum + denominations[i] <= amount{ + sum += denominations[i] + min++ + } else{ + break + } + } + } + + if sum != amount{ + return -1 + } + + return min +} + +// CoinCombination returns a map with the specific combination of coins that gives +// the minimum number. The keys are coin denominations and values are the number of +// coins used for each denomination. +// If the amount cannot be made with the given denominations, return an empty map. +func CoinCombination(amount int, denominations []int) map[int]int { + // TODO: Implement this function + c := make(map[int]int) + sum := 0 + + for i:= len(denominations) - 1; i >= 0; i--{ + for{ + if sum + denominations[i] <= amount{ + sum += denominations[i] + c[denominations[i]]++ + } else{ + break + } + } + } + + if sum != amount { + return map[int]int{} + } + + return c +}