-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
DynamicCoinChangeSolver.cs
173 lines (139 loc) · 5.3 KB
/
DynamicCoinChangeSolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.Linq;
namespace Algorithms.Problems.DynamicProgramming.CoinChange;
public static class DynamicCoinChangeSolver
{
/// <summary>
/// Generates an array of changes for current coin.
/// For instance, having coin C = 6 and array A = [1,3,4] it returns an array R = [2,3,5].
/// Because, 6 - 4 = 2, 6 - 3 = 3, 6 - 1 = 5.
/// </summary>
/// <param name="coin">The value of the coin to be exchanged.</param>
/// <param name="coins">An array of available coins.</param>
/// <returns>Array of changes of current coins by available coins.</returns>
public static int[] GenerateSingleCoinChanges(int coin, int[] coins)
{
ValidateCoin(coin);
ValidateCoinsArray(coins);
var coinsArrayCopy = new int[coins.Length];
Array.Copy(coins, coinsArrayCopy, coins.Length);
Array.Sort(coinsArrayCopy);
Array.Reverse(coinsArrayCopy);
var list = new List<int>();
foreach (var item in coinsArrayCopy)
{
if (item > coin)
{
continue;
}
var difference = coin - item;
list.Add(difference);
}
var result = list.ToArray();
return result;
}
/// <summary>
/// Given a positive integer N, such as coin.
/// Generates a change dictionary for all values [1,N].
/// Used in so-called backward induction in search of the minimum exchange.
/// </summary>
/// <param name="coin">The value of coin.</param>
/// <param name="coins">Array of available coins.</param>
/// <returns>Change dictionary for all values [1,N], where N is the coin.</returns>
public static Dictionary<int, int[]> GenerateChangesDictionary(int coin, int[] coins)
{
var dict = new Dictionary<int, int[]>();
var currentCoin = 1;
while (currentCoin <= coin)
{
var changeArray = GenerateSingleCoinChanges(currentCoin, coins);
dict[currentCoin] = changeArray;
currentCoin++;
}
return dict;
}
/// <summary>
/// Gets a next coin value, such that changes array contains the minimal change overall possible changes.
/// For example, having coin N = 6 and A = [1,3,4] coins array.
/// The minimum next coin for 6 will be 3, because changes of 3 by A = [1,3,4] contains 0, the minimal change.
/// </summary>
/// <param name="coin">Coin to be exchanged.</param>
/// <param name="exchanges">Dictionary of exchanges for [1, coin].</param>
/// <returns>Index of the next coin with minimal exchange.</returns>
public static int GetMinimalNextCoin(int coin, Dictionary<int, int[]> exchanges)
{
var nextCoin = int.MaxValue;
var minChange = int.MaxValue;
var coinChanges = exchanges[coin];
foreach (var change in coinChanges)
{
if (change == 0)
{
return 0;
}
var currentChange = exchanges[change];
var min = currentChange.Min();
var minIsLesser = min < minChange;
if (minIsLesser)
{
nextCoin = change;
minChange = min;
}
}
return nextCoin;
}
/// <summary>
/// Performs a coin change such that an amount of coins is minimal.
/// </summary>
/// <param name="coin">The value of coin to be exchanged.</param>
/// <param name="coins">An array of available coins.</param>
/// <returns>Array of exchanges.</returns>
public static int[] MakeCoinChangeDynamic(int coin, int[] coins)
{
var changesTable = GenerateChangesDictionary(coin, coins);
var list = new List<int>();
var currentCoin = coin;
var nextCoin = int.MaxValue;
while (nextCoin != 0)
{
nextCoin = GetMinimalNextCoin(currentCoin, changesTable);
var difference = currentCoin - nextCoin;
list.Add(difference);
currentCoin = nextCoin;
}
var result = list.ToArray();
return result;
}
private static void ValidateCoin(int coin)
{
if (coin <= 0)
{
throw new InvalidOperationException($"The coin cannot be lesser or equal to zero {nameof(coin)}.");
}
}
private static void ValidateCoinsArray(int[] coinsArray)
{
var coinsAsArray = coinsArray.ToArray();
if (coinsAsArray.Length == 0)
{
throw new InvalidOperationException($"Coins array cannot be empty {nameof(coinsAsArray)}.");
}
var coinsContainOne = coinsAsArray.Any(x => x == 1);
if (!coinsContainOne)
{
throw new InvalidOperationException($"Coins array must contain coin 1 {nameof(coinsAsArray)}.");
}
var containsNonPositive = coinsAsArray.Any(x => x <= 0);
if (containsNonPositive)
{
throw new InvalidOperationException(
$"{nameof(coinsAsArray)} cannot contain numbers less than or equal to zero");
}
var containsDuplicates = coinsAsArray.GroupBy(x => x).Any(g => g.Count() > 1);
if (containsDuplicates)
{
throw new InvalidOperationException($"Coins array cannot contain duplicates {nameof(coinsAsArray)}.");
}
}
}