Skip to content

Conversation

@yz4230
Copy link
Contributor

@yz4230 yz4230 commented Nov 16, 2025

Challenge 22 Solution

Submitted by: @yz4230
Challenge: Challenge 22

Description

This PR contains my solution for Challenge 22.

Changes

  • Added solution file to challenge-22/submissions/yz4230/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 16, 2025

Walkthrough

A 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

Cohort / File(s) Summary
Challenge 22 Greedy Coin Change Solution
challenge-22/submissions/yz4230/solution-template.go
New file with MinCoins() function that returns minimum coin count needed for a given amount (or -1 if impossible) and CoinCombination() function that returns a map of denomination-to-count pairs; includes a main function demonstrating both functions with US coin denominations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify that the greedy algorithm correctly handles edge cases (zero amount, no denominations, insufficient coins)
  • Confirm that the coin ordering and modulus-based calculation logic produces accurate results
  • Check for potential integer overflow or division-by-zero issues

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a solution file for Challenge 22 submitted by yz4230.
Description check ✅ Passed The description is related to the changeset, clearly documenting the Challenge 22 solution submission with details about what was added and testing verification.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 greedy MinCoins implementation

This greedy approach is correct only under certain assumptions (e.g., all denominations are 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: Simplify CoinCombination map handling and clarify impossible-case behavior

CoinCombination mirrors the greedy logic correctly, but the map handling is more complex than needed and the make call 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 when ac > 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, MinCoins and CoinCombination share 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

📥 Commits

Reviewing files that changed from the base of the PR and between 9b711a4 and 7b99c8b.

📒 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 idiomatic

The main function 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.

@RezaSi RezaSi merged commit 7660043 into RezaSi:main Nov 18, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants