1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-09 11:09:07 +01:00
CodeWars/4kyu/counting_change_combinations/solution.cs
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

28 lines
833 B
C#

using System;
public static class Kata {
public static int CountCombinations(int money, int[] coins, bool recursivelyCalled = false) {
if (money == 0 && recursivelyCalled) return 1;
else if (coins.GetLength(0) == 1) {
if (money % coins[0] == 0) return 1;
else return 0;
} else if (!recursivelyCalled) {
Array.Sort(coins);
Array.Reverse(coins);
}
var result = 0;
var times = money / coins[0];
var newCoins = new int[coins.GetLength(0) - 1];
for (var i = 0; i < coins.GetLength(0) - 1; i++)
newCoins[i] = coins[i + 1];
for (var i = 0; i <= times; i++) {
result += CountCombinations(money - i * coins[0], newCoins, true);
}
return result;
}
}