Add solution for Challenge 22 by WHFF521#1896
Conversation
WalkthroughAdds greedy ChangesCoin-change implementation
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.
🧹 Nitpick comments (2)
challenge-22/submissions/WHFF521/solution-template.go (2)
32-47: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueFormat the code to adhere to Go standards.
The code logic is correct, but it lacks idiomatic Go spacing around operators and loop constructs. Consider applying
gofmtto improve readability, and you can also safely remove the intermediatelengthvariable and theTODOcomment.♻️ Proposed formatting
- // TODO: Implement this function - sum := amount - var result int - length := len(denominations) - for i:=length-1;i>=0;i--{ - result +=sum/denominations[i] - sum =sum%denominations[i] - if sum==0{ - break - } - } - if sum!=0 { - return -1 - } - return result + sum := amount + var result int + for i := len(denominations) - 1; i >= 0; i-- { + result += sum / denominations[i] + sum %= denominations[i] + if sum == 0 { + break + } + } + if sum != 0 { + return -1 + } + return result
54-73: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick winAvoid redundant calculation and format the code.
Currently, this function calls
MinCoinsto verify if the amount can be made, which duplicates the calculation. You can eliminate the overhead by simply returning an empty map ifsum != 0at the end of the local greedy pass.Additionally, this snippet could benefit from
gofmtspacing.♻️ Proposed refactor
- // TODO: Implement this function - sum := amount - m := make(map[int]int) - if MinCoins(amount,denominations) ==-1{ - return m - } - - length := len(denominations) - for i:=length-1;i>=0;i--{ - num := sum/denominations[i] - if num == 0 { - continue - } - m[denominations[i]] = num - sum = sum%denominations[i] - if sum==0{ - break - } - } - return m + sum := amount + m := make(map[int]int) + + for i := len(denominations) - 1; i >= 0; i-- { + num := sum / denominations[i] + if num == 0 { + continue + } + m[denominations[i]] = num + sum %= denominations[i] + if sum == 0 { + break + } + } + + if sum != 0 { + return make(map[int]int) + } + + return m
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 19de8ca0-9bc7-40b6-9cbd-3edb95a785b2
📒 Files selected for processing (1)
challenge-22/submissions/WHFF521/solution-template.go
Challenge 22 Solution
Submitted by: @WHFF521
Challenge: Challenge 22
Description
This PR contains my solution for Challenge 22.
Changes
challenge-22/submissions/WHFF521/solution-template.goTesting
Thank you for reviewing my submission! 🚀