Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Nov 27, 2024
1 parent 4064744 commit 0d8a57b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions LeetCode/src/LeetCode.Challenges/CoinChange/SolutionV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// This is an optimized solution based on a bottom-up dynamic programming approach instead of recursion.
// This approach iterates through each possible amount from 1 to the target amount,
// calculating the minimum number of coins needed to make up each amount.
public class SolutionV2
public static class SolutionV2
{
public int CoinChange(int[] coins, int amount)
public static int CoinChange(int[] coins, int amount)
{
// Initialize an array to store the minimum number of coins needed for each amount.
var amountMins = new int[amount + 1];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using LeetCode.Challenges.CoinChange;
using Xunit;
using Shouldly;

namespace LeetCode.Challenges.UnitTests.CoinChange;

public class SolutionV2Tests
{
[Theory]
[MemberData(nameof(CoinChangeTestCases))]
public void CoinChange_ShouldReturnExpectedResult(int[] coins, int amount, int expected)
{
SolutionV2.CoinChange(coins, amount).ShouldBe(expected);
}

public static IEnumerable<object[]> CoinChangeTestCases()
{
yield return [new[] { 1, 2, 5 }, 11, 3];
yield return [new[] { 2 }, 3, -1];
yield return [new[] { 1 }, 0, 0];
yield return [new[] { 186, 419, 83, 408 }, 6249, 20];
yield return [new[] { 1, 3, 4 }, 6, 2];
yield return [new[] { 5 }, 5, 1];
yield return [new[] { 5 }, 0, 0];
yield return [new[] { 5 }, 2, -1];
yield return [new[] { 2, 5, 10 }, 1, -1];
yield return [new[] { 10, 5 }, 20, 2];
yield return [new[] { 1, 5, 10, 25 }, 30, 2];
yield return [new[] { 2, 3 }, 7, 3];
yield return [new[] { 1, 2, 5 }, 100, 20];
yield return [new[] { 7, 3, 2 }, 8, 3];
yield return [new[] { 1, 7, 10 }, 14, 2];
yield return [new[] { 1, 3, 4, 7 }, 15, 3];
yield return [new[] { 25, 50, 100 }, 30, -1];
yield return [new[] { 9, 6, 5, 1 }, 11, 2];
}
}

0 comments on commit 0d8a57b

Please sign in to comment.