From f8d8930f07a7f9cb4bb2bece6cf0d1dd32afe80d Mon Sep 17 00:00:00 2001 From: "go-interview-practice-bot[bot]" <230190823+go-interview-practice-bot[bot]@users.noreply.github.com> Date: Wed, 8 Jul 2026 12:32:08 +0000 Subject: [PATCH 1/2] Add solution for Challenge 22 --- .../submissions/Kosench/solution-template.go | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 challenge-22/submissions/Kosench/solution-template.go diff --git a/challenge-22/submissions/Kosench/solution-template.go b/challenge-22/submissions/Kosench/solution-template.go new file mode 100644 index 000000000..964b9a9d5 --- /dev/null +++ b/challenge-22/submissions/Kosench/solution-template.go @@ -0,0 +1,88 @@ +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("---------------------------") + } +} + +func MinCoins(amount int, denominations []int) int { + if amount < 0 { + return -1 + } + if amount == 0 { + return 0 + } + + totalCoins := 0 + remaining := amount + + // Идем с конца, так как жадный алгоритм требует начинать с самой крупной монеты + for i := len(denominations) - 1; i >= 0; i-- { + coin := denominations[i] + if coin > 0 && coin <= remaining { + count := remaining / coin + totalCoins += count + remaining %= coin // Эквивалентно remaining -= count * coin, но чище + } + if remaining == 0 { + break + } + } + + // Если после прохода всех монет остаток всё еще есть, набрать сумму невозможно + if remaining > 0 { + return -1 + } + return totalCoins +} + +// 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 { + combo := make(map[int]int) + if amount < 0 { + return combo + } + + remaining := amount + + for i := len(denominations) - 1; i >= 0; i-- { + coin := denominations[i] + if coin > 0 && coin <= remaining { + count := remaining / coin + combo[coin] = count + remaining %= coin + } + if remaining == 0 { + break + } + } + + if remaining > 0 { + return make(map[int]int) // Возвращаем пустой map, как требуется в условии + } + return combo +} From bb5101b701c6bcd3bbff558cefc8dadecffd9a45 Mon Sep 17 00:00:00 2001 From: Kosench Date: Wed, 8 Jul 2026 22:45:02 +1000 Subject: [PATCH 2/2] fix --- .../submissions/Kosench/solution-template.go | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/challenge-22/submissions/Kosench/solution-template.go b/challenge-22/submissions/Kosench/solution-template.go index 964b9a9d5..49b70bfa4 100644 --- a/challenge-22/submissions/Kosench/solution-template.go +++ b/challenge-22/submissions/Kosench/solution-template.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "sort" ) func main() { @@ -34,43 +35,44 @@ func MinCoins(amount int, denominations []int) int { return 0 } + sorted := append([]int(nil), denominations...) + sort.Ints(sorted) + totalCoins := 0 remaining := amount - // Идем с конца, так как жадный алгоритм требует начинать с самой крупной монеты - for i := len(denominations) - 1; i >= 0; i-- { - coin := denominations[i] + for i := len(sorted) - 1; i >= 0; i-- { + coin := sorted[i] if coin > 0 && coin <= remaining { count := remaining / coin totalCoins += count - remaining %= coin // Эквивалентно remaining -= count * coin, но чище + remaining %= coin } if remaining == 0 { break } } - // Если после прохода всех монет остаток всё еще есть, набрать сумму невозможно if remaining > 0 { return -1 } return totalCoins } -// 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. +// CoinCombination returns a map with the specific combination of coins. func CoinCombination(amount int, denominations []int) map[int]int { combo := make(map[int]int) if amount < 0 { return combo } + sorted := append([]int(nil), denominations...) + sort.Ints(sorted) + remaining := amount - for i := len(denominations) - 1; i >= 0; i-- { - coin := denominations[i] + for i := len(sorted) - 1; i >= 0; i-- { + coin := sorted[i] if coin > 0 && coin <= remaining { count := remaining / coin combo[coin] = count @@ -82,7 +84,7 @@ func CoinCombination(amount int, denominations []int) map[int]int { } if remaining > 0 { - return make(map[int]int) // Возвращаем пустой map, как требуется в условии + return make(map[int]int) } return combo }