Skip to content

Comments

322. Coin Change#25

Merged
yamcodes merged 9 commits intomainfrom
coin-change
Feb 18, 2026
Merged

322. Coin Change#25
yamcodes merged 9 commits intomainfrom
coin-change

Conversation

@yamcodes
Copy link
Owner

@yamcodes yamcodes commented Feb 18, 2026

Summary by CodeRabbit

Release Notes

  • New Features

    • Added LeetCode problem 322 (Coin Change) solution implementation to the solutions library.
  • Tests

    • Added parameterized test cases covering multiple scenarios for the new solution.
  • Documentation

    • Updated README with new problem entry and reference links; added solution documentation.

@coderabbitai
Copy link

coderabbitai bot commented Feb 18, 2026

Caution

Review failed

The pull request is closed.

Walkthrough

This pull request introduces a new LeetCode problem solution for Problem 322 (Coin Change). It adds documentation, two dynamic programming implementations (naive and optimized), parameterized test utilities, and updates the README with the new problem entry and references.

Changes

Cohort / File(s) Summary
Documentation
README.md, src/main/java/codes/yam/leetcode/coinchange/package-info.java
Updated README with LeetCode 322 entry and references; added package-info.java with Javadoc documenting the coin change package, slug, difficulty, and solution progression.
Solution Implementations
src/main/java/codes/yam/leetcode/coinchange/Solution.java, src/main/java/codes/yam/leetcode/coinchange/SolutionNaive.java
Introduced two DP-based implementations: optimized solution using standard DP approach and naive solution using sentinel-based bottom-up DP; both with O(amount × n) time and O(amount) space complexity.
Test Infrastructure
src/test/java/codes/yam/leetcode/coinchange/TestCases.java, src/test/java/codes/yam/leetcode/coinchange/SolutionNaiveTest.java, src/test/java/codes/yam/leetcode/coinchange/SolutionTest.java
Added TestCases utility class providing three parameterized test scenarios; created separate parameterized test classes for both Solution and SolutionNaive using JUnit 5 annotations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • 322. Coin Change #25: Introduces coin-change implementations with DP-style classes and coinChange(int[], int) methods in the same package, sharing the same organizational structure and approach.

Poem

🐰 A coin change, oh what a delight!
Dynamic programming shines so bright,
With tests and docs all in their place,
This solution runs at perfect pace! 💰

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the specific LeetCode problem being solved (322. Coin Change) and aligns with the main changes: adding solution implementations, tests, and documentation for this problem.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch coin-change

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.

@yamcodes yamcodes changed the title Coin change 322. Coin Change Feb 18, 2026
@yamcodes yamcodes marked this pull request as ready for review February 18, 2026 19:48
Copy link

@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: 4

🧹 Nitpick comments (2)
src/main/java/codes/yam/leetcode/coinchange/SolutionDpNaive.java (1)

4-5: Redundant amount == 0 guard — the general logic handles this correctly.

dp[0] defaults to 0, the loop body for (int a = 1; a <= 0; ...) never executes, and dp[0] is returned. The early return adds noise without defensive value.

♻️ Proposed simplification
 int coinChange(int[] coins, int amount) {
-  if (amount == 0) return 0;
   int[] dp = new int[amount + 1];
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/codes/yam/leetcode/coinchange/SolutionDpNaive.java` around
lines 4 - 5, Remove the redundant early return in coinChange: delete the "if
(amount == 0) return 0;" guard in SolutionDpNaive.coinChange and rely on the
existing dp array logic (ensure dp is sized amount+1 with dp[0] == 0, the for
loop from a = 1..amount will skip when amount==0, and the final return logic
using dp[amount] stays unchanged).
src/test/java/codes/yam/leetcode/coinchange/TestCases.java (1)

8-13: LGTM — three well-chosen scenarios covering the key paths.

Consider adding a case where no coin divides the amount (already covered by [2]/3 → -1) and one where a coin exactly equals amount (coins=[11], amount=11 → 1) to reinforce the c == a path tested specifically by SolutionDpNaive.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/test/java/codes/yam/leetcode/coinchange/TestCases.java` around lines 8 -
13, Add a test case to cover the path where a coin exactly equals the amount by
updating the TestCases.cases() Stream to include Arguments.of(new int[] {11},
11, 1); this ensures the SolutionDpNaive/c == a branch is exercised; simply
append that Arguments.of entry alongside the existing cases in the
Stream.of(...) list.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@README.md`:
- Line 14: Update the README table row for lc-322 / SolutionDp to replace both
"TBD" entries with the correct complexities: set Time to "O(amount × n) where n
= coins.length" and set Space to "O(amount)"; locate the table row referencing
SolutionDp (Coin Change) and edit the Time and Space columns accordingly so they
match the other rows' format.

In `@src/main/java/codes/yam/leetcode/coinchange/SolutionDp.java`:
- Line 5: The file defines class SolutionDp but the repository requires a
canonical Solution.java for the optimal solution; either rename the file and
class from SolutionDp to Solution (i.e., change class SolutionDp -> Solution and
the filename SolutionDp.java -> Solution.java) or create a new Solution.java
that forwards to or contains the implementation in SolutionDp (for example by
copying the implementation or delegating calls to SolutionDp), ensuring the
package and public class name are exactly Solution so the canonical entrypoint
exists alongside any SolutionDp variant.
- Around line 5-20: Add an HTML-formatted Javadoc block immediately above the
class declaration for SolutionDp describing the class and include explicit `@Time`
Complexity and `@Space` Complexity tags (e.g., "Time Complexity: O(amount *
coins.length)" and "Space Complexity: O(amount)") documenting the
coinChange(int[] coins, int amount) DP solution; ensure the Javadoc uses
standard HTML tags and adheres to the package-scoped Solution*.java guidelines.

In `@src/main/java/codes/yam/leetcode/coinchange/SolutionDpNaive.java`:
- Line 3: Add an HTML-formatted Javadoc block immediately above the class
declaration for SolutionDpNaive that documents the algorithm and includes
explicit "Time Complexity" and "Space Complexity" lines (e.g., using <p> or <b>
tags to label them) so the class follows the project guideline for Solution*
classes; keep the class package-scoped and retain the existing class name
SolutionDpNaive while filling in the accurate Big-O complexities for the
implemented DP naive coin change approach.

---

Nitpick comments:
In `@src/main/java/codes/yam/leetcode/coinchange/SolutionDpNaive.java`:
- Around line 4-5: Remove the redundant early return in coinChange: delete the
"if (amount == 0) return 0;" guard in SolutionDpNaive.coinChange and rely on the
existing dp array logic (ensure dp is sized amount+1 with dp[0] == 0, the for
loop from a = 1..amount will skip when amount==0, and the final return logic
using dp[amount] stays unchanged).

In `@src/test/java/codes/yam/leetcode/coinchange/TestCases.java`:
- Around line 8-13: Add a test case to cover the path where a coin exactly
equals the amount by updating the TestCases.cases() Stream to include
Arguments.of(new int[] {11}, 11, 1); this ensures the SolutionDpNaive/c == a
branch is exercised; simply append that Arguments.of entry alongside the
existing cases in the Stream.of(...) list.


import java.util.Arrays;

class SolutionDp {
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

SolutionDp should be Solution.java — the canonical final/optimal solution file is missing.

The guidelines require each problem package to contain a Solution.java as the final/optimal solution, with additional variants as Solution*.java. Currently SolutionDp.java plays the role of the optimal solution (as documented in package-info.java), but no Solution.java exists. Either rename SolutionDp.javaSolution.java, or add a dedicated Solution.java that contains the optimal implementation.

As per coding guidelines, src/main/java/codes/yam/leetcode/**/*.java: "Each LeetCode problem should live in its own package under codes.yam.leetcode with Solution.java as the final/optimal solution and additional solutions prefixed with Solution*."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/codes/yam/leetcode/coinchange/SolutionDp.java` at line 5, The
file defines class SolutionDp but the repository requires a canonical
Solution.java for the optimal solution; either rename the file and class from
SolutionDp to Solution (i.e., change class SolutionDp -> Solution and the
filename SolutionDp.java -> Solution.java) or create a new Solution.java that
forwards to or contains the implementation in SolutionDp (for example by copying
the implementation or delegating calls to SolutionDp), ensuring the package and
public class name are exactly Solution so the canonical entrypoint exists
alongside any SolutionDp variant.

Comment on lines 5 to 20
class SolutionDp {
int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, 1, amount + 1, amount + 1);
for (int a = 1; a <= amount; a++) {
for (int c : coins) {
if (c <= a) {
dp[a] = Math.min(dp[a], 1 + dp[a - c]);
}
}
}
if (dp[amount] > amount) {
return -1;
}
return dp[amount];
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Add required Javadoc with Time and Space Complexity tags.

Both SolutionDp and the broader package are missing the required HTML-formatted Javadoc block. Per the coding guidelines, every Solution*.java must document Time and Space Complexity.

📝 Proposed Javadoc addition
+/**
+ * Bottom-up DP using {`@code` amount + 1} as the infinity sentinel.
+ *
+ * <p><b>Time Complexity:</b> O(amount &times; coins.length)
+ *
+ * <p><b>Space Complexity:</b> O(amount)
+ */
 class SolutionDp {

As per coding guidelines, src/main/java/codes/yam/leetcode/**/Solution*.java: "Solution classes must be package-scoped (not public), use prefix naming (Solution, SolutionNaive, SolutionDp, etc.), and include HTML-formatted Javadoc with Time Complexity and Space Complexity tags."

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
class SolutionDp {
int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, 1, amount + 1, amount + 1);
for (int a = 1; a <= amount; a++) {
for (int c : coins) {
if (c <= a) {
dp[a] = Math.min(dp[a], 1 + dp[a - c]);
}
}
}
if (dp[amount] > amount) {
return -1;
}
return dp[amount];
}
/**
* Bottom-up DP using {`@code` amount + 1} as the infinity sentinel.
*
* <p><b>Time Complexity:</b> O(amount &times; coins.length)
*
* <p><b>Space Complexity:</b> O(amount)
*/
class SolutionDp {
int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, 1, amount + 1, amount + 1);
for (int a = 1; a <= amount; a++) {
for (int c : coins) {
if (c <= a) {
dp[a] = Math.min(dp[a], 1 + dp[a - c]);
}
}
}
if (dp[amount] > amount) {
return -1;
}
return dp[amount];
}
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/codes/yam/leetcode/coinchange/SolutionDp.java` around lines 5 -
20, Add an HTML-formatted Javadoc block immediately above the class declaration
for SolutionDp describing the class and include explicit `@Time` Complexity and
`@Space` Complexity tags (e.g., "Time Complexity: O(amount * coins.length)" and
"Space Complexity: O(amount)") documenting the coinChange(int[] coins, int
amount) DP solution; ensure the Javadoc uses standard HTML tags and adheres to
the package-scoped Solution*.java guidelines.

@@ -0,0 +1,26 @@
package codes.yam.leetcode.coinchange;

class SolutionDpNaive {
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Add required Javadoc with Time and Space Complexity tags.

Same guideline violation as SolutionDp.java.

📝 Proposed Javadoc addition
+/**
+ * Bottom-up DP using {`@code` -1} as the sentinel for unreachable states.
+ *
+ * <p><b>Time Complexity:</b> O(amount &times; coins.length)
+ *
+ * <p><b>Space Complexity:</b> O(amount)
+ */
 class SolutionDpNaive {

As per coding guidelines, src/main/java/codes/yam/leetcode/**/Solution*.java: "Solution classes must be package-scoped (not public), use prefix naming (Solution, SolutionNaive, SolutionDp, etc.), and include HTML-formatted Javadoc with Time Complexity and Space Complexity tags."

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
class SolutionDpNaive {
/**
* Bottom-up DP using {`@code` -1} as the sentinel for unreachable states.
*
* <p><b>Time Complexity:</b> O(amount &times; coins.length)
*
* <p><b>Space Complexity:</b> O(amount)
*/
class SolutionDpNaive {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/codes/yam/leetcode/coinchange/SolutionDpNaive.java` at line 3,
Add an HTML-formatted Javadoc block immediately above the class declaration for
SolutionDpNaive that documents the algorithm and includes explicit "Time
Complexity" and "Space Complexity" lines (e.g., using <p> or <b> tags to label
them) so the class follows the project guideline for Solution* classes; keep the
class package-scoped and retain the existing class name SolutionDpNaive while
filling in the accurate Big-O complexities for the implemented DP naive coin
change approach.

@yamcodes yamcodes merged commit 5c9e9ec into main Feb 18, 2026
1 of 2 checks passed
@yamcodes yamcodes deleted the coin-change branch February 18, 2026 19:58
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