Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions challenge-22/submissions/nzamulov/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 {
if amount == 0 {
return 0
}
c := CoinCombination(amount, denominations)
if len(c) == 0 {
return -1
}
m := 0
for _, val := range c {
m += val
}
return m
}

// 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 {
m := make(map[int]int)
for i := len(denominations) - 1; amount > 0 && i >= 0; i-- {
for amount > 0 && denominations[i] <= amount {
amount -= denominations[i]
m[denominations[i]]++
}
}
return m
}
Comment on lines +50 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical bug: doesn't verify exact change was made.

According to the documentation (line 49), the function should return an empty map if the amount cannot be made with the given denominations. However, after the loops complete at line 57, if amount > 0, it means there's a remainder that couldn't be satisfied with the available denominations. The function should return an empty map in this case, not a partial result.

Example: With denominations = [3, 5] and amount = 7, the function would return {5: 1} with a remainder of 2, which is incorrect.

Apply this diff to fix:

 func CoinCombination(amount int, denominations []int) map[int]int {
     m := make(map[int]int)
 	for i := len(denominations) - 1; amount > 0 && i >= 0; i-- {
 	    for amount > 0 && denominations[i] <= amount {
 	        amount -= denominations[i]
 	        m[denominations[i]]++
 	    }
 	}
+	if amount > 0 {
+	    return make(map[int]int) // Return empty map if exact change not possible
+	}
 	return m
 }
🤖 Prompt for AI Agents
In challenge-22/submissions/nzamulov/solution-template.go around lines 50 to 59,
the function currently returns a partial coin map when the amount cannot be
exactly made; after the greedy loops finish you must verify exact change was
achieved and if amount > 0 return an empty map instead of the partial result —
implement a post-loop check that if amount != 0 then return make(map[int]int)
(an empty map), otherwise return the constructed m.

Loading