From 252b58997c5437210ef136d8f0e699b6c3b88d00 Mon Sep 17 00:00:00 2001 From: eminencegrs Date: Fri, 13 Dec 2024 23:26:36 +0100 Subject: [PATCH] Refactoring. --- .../CoinChange/Description.md | 3 +- .../CoinChange/SolutionV1.cs | 8 ++-- .../CoinChange/SolutionV1Tests.cs | 37 +++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 LeetCode/tests/LeetCode.Challenges.UnitTests/CoinChange/SolutionV1Tests.cs diff --git a/LeetCode/src/LeetCode.Challenges/CoinChange/Description.md b/LeetCode/src/LeetCode.Challenges/CoinChange/Description.md index 1020c57..c1a98e5 100644 --- a/LeetCode/src/LeetCode.Challenges/CoinChange/Description.md +++ b/LeetCode/src/LeetCode.Challenges/CoinChange/Description.md @@ -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. diff --git a/LeetCode/src/LeetCode.Challenges/CoinChange/SolutionV1.cs b/LeetCode/src/LeetCode.Challenges/CoinChange/SolutionV1.cs index a22ad4f..f50f81c 100644 --- a/LeetCode/src/LeetCode.Challenges/CoinChange/SolutionV1.cs +++ b/LeetCode/src/LeetCode.Challenges/CoinChange/SolutionV1.cs @@ -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(); + return GetChange(amount); + int GetChange(int tempAmount) { if (tempAmount == 0) @@ -36,7 +38,5 @@ int GetChange(int tempAmount) cache[tempAmount] = minCoins != int.MaxValue ? minCoins : -1; return cache[tempAmount]; } - - return GetChange(amount); } } diff --git a/LeetCode/tests/LeetCode.Challenges.UnitTests/CoinChange/SolutionV1Tests.cs b/LeetCode/tests/LeetCode.Challenges.UnitTests/CoinChange/SolutionV1Tests.cs new file mode 100644 index 0000000..1aa9275 --- /dev/null +++ b/LeetCode/tests/LeetCode.Challenges.UnitTests/CoinChange/SolutionV1Tests.cs @@ -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 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]; + } +}