Skip to content

Add solution for Challenge 22 by Kosench#1878

Merged
github-actions[bot] merged 2 commits into
RezaSi:mainfrom
Kosench:challenge-22-Kosench
Jul 9, 2026
Merged

Add solution for Challenge 22 by Kosench#1878
github-actions[bot] merged 2 commits into
RezaSi:mainfrom
Kosench:challenge-22-Kosench

Conversation

@Kosench

@Kosench Kosench commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Challenge 22 Solution

Submitted by: @Kosench
Challenge: Challenge 22

Description

This PR contains my solution for Challenge 22.

Changes

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

Testing

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

Thank you for reviewing my submission! 🚀

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 33852238-85f7-4c45-ba4d-0d2956c9cfde

📥 Commits

Reviewing files that changed from the base of the PR and between f8d8930 and bb5101b.

📒 Files selected for processing (1)
  • challenge-22/submissions/Kosench/solution-template.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • challenge-22/submissions/Kosench/solution-template.go

Walkthrough

Adds a Go solution file for Challenge 22 with greedy coin-change functions and a sample main that prints results for fixed amounts using U.S. denominations.

Changes

Greedy Coin-Change Solution

Layer / File(s) Summary
Greedy coin-change functions
challenge-22/submissions/Kosench/solution-template.go
Defines MinCoins and CoinCombination using sorted descending denominations, with -1/empty-map handling when an amount cannot be formed exactly.
Demo entrypoint
challenge-22/submissions/Kosench/solution-template.go
Adds main that iterates sample amounts, calls both functions, and prints the outputs.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Poem

I’m a bunny with a coin-count grin,
Hop, sort, and pick the biggest in.
Quarters first, then dimes do gleam,
Tiny pennies finish the stream.
🐇🪙

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: adding a solution for Challenge 22 by Kosench.
Description check ✅ Passed The description is directly related to the submitted Challenge 22 solution and its testing.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
challenge-22/submissions/Kosench/solution-template.go (1)

29-58: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Extract shared greedy logic to reduce duplication.

MinCoins and CoinCombination contain 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

📥 Commits

Reviewing files that changed from the base of the PR and between f5e7ce5 and f8d8930.

📒 Files selected for processing (1)
  • challenge-22/submissions/Kosench/solution-template.go

Comment thread challenge-22/submissions/Kosench/solution-template.go Outdated
@github-actions
github-actions Bot merged commit 36d0ac4 into RezaSi:main Jul 9, 2026
6 checks passed
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

🎉 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.

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.

1 participant