Add solution for Challenge 22 by Kosench#1878
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughAdds a Go solution file for Challenge 22 with greedy coin-change functions and a sample ChangesGreedy Coin-Change Solution
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
challenge-22/submissions/Kosench/solution-template.go (1)
29-58: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winExtract shared greedy logic to reduce duplication.
MinCoinsandCoinCombinationcontain nearly identical greedy loops (lines 41-51 and 72-82). Extracting a shared helper that returns both the total count and the denomination-to-count map would eliminate duplication and ensure both functions stay in sync if the algorithm changes.♻️ Proposed refactor: shared greedy helper
+// greedyBreakdown runs the greedy coin-selection and returns the total coin +// count and a denomination-to-count map. If the amount cannot be made, count is -1 +// and the map is empty. +func greedyBreakdown(amount int, denominations []int) (int, map[int]int) { + combo := make(map[int]int) + if amount < 0 { + return -1, combo + } + remaining := amount + totalCoins := 0 + + sorted := make([]int, len(denominations)) + copy(sorted, denominations) + sort.Ints(sorted) + + for i := len(sorted) - 1; i >= 0; i-- { + coin := sorted[i] + if coin > 0 && coin <= remaining { + count := remaining / coin + combo[coin] = count + totalCoins += count + remaining %= coin + } + if remaining == 0 { + break + } + } + if remaining > 0 { + return -1, make(map[int]int) + } + return totalCoins, combo +} + 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 - } - if remaining == 0 { - break - } - } - - if remaining > 0 { - return -1 - } - return totalCoins + count, _ := greedyBreakdown(amount, denominations) + if amount == 0 { + return 0 + } + return count } 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) - } - return combo + _, combo := greedyBreakdown(amount, denominations) + return combo }Also applies to: 64-88
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: b72cdab8-3c18-446e-9f9b-669e729335fd
📒 Files selected for processing (1)
challenge-22/submissions/Kosench/solution-template.go
|
🎉 Auto-merged! This PR was automatically merged after 2 days with all checks passing. Thank you for your contribution, @Kosench! 📊 Scoreboards and badges will be updated shortly. |
Challenge 22 Solution
Submitted by: @Kosench
Challenge: Challenge 22
Description
This PR contains my solution for Challenge 22.
Changes
challenge-22/submissions/Kosench/solution-template.goTesting
Thank you for reviewing my submission! 🚀