-
-
Notifications
You must be signed in to change notification settings - Fork 712
Add solution for Challenge 22 by yz4230 #748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughA new Go solution file is introduced for Challenge 22 that implements coin change using a greedy algorithm. Two exported functions compute the minimum coin count and the specific coin denomination breakdown, with a main function demonstrating usage on test amounts. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
challenge-22/submissions/yz4230/solution-template.go (2)
31-41: Call out assumptions behind the greedyMinCoinsimplementationThis greedy approach is correct only under certain assumptions (e.g., all
denominationsare positive, non-zero, and ordered so that iterating from the end uses largest to smallest values; and the coin system is one where greedy is optimal). It may return a non-minimal count for arbitrary denomination sets or misbehave if a zero/negative value is included. Consider either documenting these assumptions in the comment or adding light validation to guard against obviously bad inputs.For example, to make the expectations explicit:
-// 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. +// MinCoins returns the minimum number of coins computed by a greedy algorithm +// using the given denominations. It assumes all denominations are positive and +// that the slice is sorted in ascending order so the greedy strategy is +// optimal for this coin system. If the amount cannot be made, it returns -1. func MinCoins(amount int, denominations []int) int {Please double‑check the challenge specification: if it requires correctness for arbitrary denomination sets (not just canonical ones like US coins), a dynamic programming solution will be needed instead of greedy.
48-66: SimplifyCoinCombinationmap handling and clarify impossible-case behavior
CoinCombinationmirrors the greedy logic correctly, but the map handling is more complex than needed and themakecall passes an unnecessary zero capacity. Since each denomination is visited at most once, there’s no need to read/accumulate an existing value; you can assign directly whenac > 0.A simpler version:
-func CoinCombination(amount int, denominations []int) map[int]int { - ret := make(map[int]int, 0) +func CoinCombination(amount int, denominations []int) map[int]int { + ret := make(map[int]int) for i := range denominations { c := denominations[len(denominations)-i-1] ac := amount / c - if ac == 0 { - continue - } - if retc, ok := ret[c]; ok { - ret[c] = retc + ac - } else { - ret[c] = ac - } - amount = amount % c + if ac == 0 { + continue + } + ret[c] = ac + amount %= c } if amount > 0 { return map[int]int{} } return ret }Also,
MinCoinsandCoinCombinationshare the same greedy assumptions; consider echoing the same assumptions in this function’s comment for consistency.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-22/submissions/yz4230/solution-template.go(1 hunks)
🔇 Additional comments (1)
challenge-22/submissions/yz4230/solution-template.go (1)
7-27: Main demo is clear and idiomaticThe
mainfunction nicely demonstrates both APIs with realistic US denominations and varied amounts; the control flow and printing are straightforward and idiomatic for a small challenge program.
Challenge 22 Solution
Submitted by: @yz4230
Challenge: Challenge 22
Description
This PR contains my solution for Challenge 22.
Changes
challenge-22/submissions/yz4230/solution-template.goTesting
Thank you for reviewing my submission! 🚀