Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Dec 13, 2024
1 parent aece2f2 commit 252b589
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
3 changes: 2 additions & 1 deletion LeetCode/src/LeetCode.Challenges/CoinChange/Description.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Coin Change

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
You are given an integer array `coins` representing coins of different denominations
and an integer `amount` representing a total amount of money.
Return the fewest number of coins that you need to make up that amount.
If that amount of money cannot be made up by any combination of the coins, return -1.

Expand Down
8 changes: 4 additions & 4 deletions LeetCode/src/LeetCode.Challenges/CoinChange/SolutionV1.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace LeetCode.Challenges.CoinChange;

public class SolutionV1
public static class SolutionV1
{
public int CoinChange(int[] coins, int amount)
public static int CoinChange(int[] coins, int amount)
{
var cache = new Dictionary<int, int>();

return GetChange(amount);

int GetChange(int tempAmount)
{
if (tempAmount == 0)
Expand Down Expand Up @@ -36,7 +38,5 @@ int GetChange(int tempAmount)
cache[tempAmount] = minCoins != int.MaxValue ? minCoins : -1;
return cache[tempAmount];
}

return GetChange(amount);
}
}
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 SolutionV1Tests
{
[Theory]
[MemberData(nameof(CoinChangeTestCases))]
public void GivenCoins_WhenCoinChange_ThenResultAsExpected(int[] coins, int amount, int expected)
{
SolutionV1.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 252b589

Please sign in to comment.