Skip to content
Merged
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
79 changes: 79 additions & 0 deletions challenge-22/submissions/Johrespi/solution-template.go
Original file line number Diff line number Diff line change
@@ -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
}
Comment on lines +58 to +79
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Significant code duplication with MinCoins.

This function duplicates the core greedy logic from MinCoins (lines 36-45 vs lines 63-71). The same greedy algorithm issues and input validation concerns apply here.

Consider refactoring to eliminate duplication:

func CoinCombination(amount int, denominations []int) map[int]int {
	combination := make(map[int]int)
	remaining := amount
	
	// Iterate from largest to smallest denomination
	for i := len(denominations) - 1; i >= 0; i-- {
		coin := denominations[i]
		if coin <= remaining {
			count := remaining / coin
			if count > 0 {
				combination[coin] = count
				remaining -= count * coin
			}
		}
	}
	
	if remaining != 0 {
		return map[int]int{}
	}
	
	return combination
}

This refactored version:

  • Uses division instead of nested loops for better efficiency
  • Eliminates the duplicate loop logic
  • More clearly expresses intent
  • Removes the TODO comment (line 59)

Note: The greedy algorithm limitation mentioned in the MinCoins review also applies here.

🤖 Prompt for AI Agents
In challenge-22/submissions/Johrespi/solution-template.go around lines 58 to 79,
CoinCombination duplicates the greedy loop logic from MinCoins and uses
inefficient nested loops; replace the nested loop with a single-pass
division-based greedy approach: iterate denominations from largest to smallest,
compute count := remaining / coin, set combination[coin] = count if >0, subtract
count*coin from remaining, and after the loop return an empty map if remaining
!= 0; remove the TODO comment and consider extracting shared greedy logic into a
helper to eliminate duplication with MinCoins.

Loading