-
Notifications
You must be signed in to change notification settings - Fork 1
/
BreakAmountUniquely.cpp
159 lines (125 loc) · 4.67 KB
/
BreakAmountUniquely.cpp
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
/*
T(n, k) = O(n * k)
M(n, k) = O(n * k)
n - liczba nominałów
k - kwota
*/
#include <iostream>
#include <vector>
class CurrentAmount {
public:
int lastUnique;
bool* possibleBreak;
bool unique;
int uniqueBreakerBanknoteIndex;
CurrentAmount() {}
CurrentAmount(int size) {
possibleBreak = new bool[size]{0};
}
~CurrentAmount() {
delete[] possibleBreak;
}
};
CurrentAmount** GetAmountsTable(const std::vector<int>& banknotes, int amount) {
CurrentAmount** amounts = new CurrentAmount*[amount + 1];
for (int i = 0; i <= amount; i++) {
amounts[i] = new CurrentAmount(banknotes.size());
}
amounts[0]->unique = true;
return amounts;
}
void SetPossibleBreaks(const std::vector<int>& banknotes, CurrentAmount** amounts, int currentAmount) {
for (int banknoteIndex = 0; banknoteIndex < banknotes.size(); banknoteIndex++) {
amounts[currentAmount]->possibleBreak[banknoteIndex] =
(currentAmount < banknotes[banknoteIndex] || !amounts[currentAmount - banknotes[banknoteIndex]]->unique)
? false
: true;
}
}
int GetSumOfPossibleBreaks(const std::vector<int>& banknotes, CurrentAmount** amounts, int currentAmount) {
int sum = 0;
for (int banknoteIndex = banknotes.size() - 1; banknoteIndex >= 0; banknoteIndex--) {
if (amounts[currentAmount]->possibleBreak[banknoteIndex] == 1) {
sum++;
amounts[currentAmount]->uniqueBreakerBanknoteIndex = banknoteIndex;
}
}
return sum;
}
// 15 = 7 + 8 = 8 + 7
int SubstractTheSameSetduplicates(const std::vector<int>& banknotes, CurrentAmount** amounts, int currentAmount) {
bool* visited = new bool[banknotes.size()]{false};
for (int banknoteIndex = 0; banknoteIndex < banknotes.size(); banknoteIndex++) {
if (visited[banknoteIndex]) continue;
visited[banknoteIndex] = true;
if (!amounts[currentAmount]->possibleBreak[banknoteIndex]) continue;
banknoteIndex = amounts[currentAmount - banknotes[banknoteIndex]]->uniqueBreakerBanknoteIndex;
while (!visited[banknoteIndex]) {
visited[banknoteIndex] = true;
amounts[currentAmount]->possibleBreak[banknoteIndex] = false;
banknoteIndex = amounts[currentAmount - banknotes[banknoteIndex]]->uniqueBreakerBanknoteIndex;
}
}
return GetSumOfPossibleBreaks(banknotes, amounts, currentAmount);
}
void SetUniqueFlag(CurrentAmount** amounts, int currentAmount, int sum) {
amounts[currentAmount]->unique =
(sum == 1)
? true
: false;
}
void SetLastUniqueValue(CurrentAmount** amounts, int currentAmount, int sum) {
amounts[currentAmount]->lastUnique =
(amounts[currentAmount]->unique)
? currentAmount
: amounts[currentAmount - 1]->lastUnique;
}
int GetHighestUniqueAmonut(int amount, CurrentAmount** amounts) {
for (int currentAmount = amount; currentAmount >= 0; currentAmount--) {
if (amounts[currentAmount]->unique)
return currentAmount;
}
}
void Print(const std::vector<int>& banknotes, int amount, CurrentAmount** amounts) {
std::cout << "\t";
for (int j = 0; j < banknotes.size(); j++) {
std::cout << banknotes[j] << " ";
}
std::cout << "\n";
for (int i = 1; i <= amount; i++) {
std::cout << i << ":\t";
for (int j = 0; j < banknotes.size(); j++) {
std::cout << amounts[i]->possibleBreak[j] << " ";
}
std::cout << "\n";
}
}
void DeleteAmounts(int amount, CurrentAmount** amounts) {
for (int i = 0; i <= amount; i++) {
delete amounts[i];
}
delete[] amounts;
}
int BreakAmountUniquely(const std::vector<int>& banknotes, int amount, bool debug) {
if (amount < banknotes[0])
return 0;
int highestUniqueAmonut, sum;
CurrentAmount** amounts = GetAmountsTable(banknotes, amount);
for (int currentAmount = 1; currentAmount <= amount; currentAmount++) {
SetPossibleBreaks(banknotes, amounts, currentAmount);
sum = GetSumOfPossibleBreaks(banknotes, amounts, currentAmount);
if (sum > 1)
sum = SubstractTheSameSetduplicates(banknotes, amounts, currentAmount);
SetUniqueFlag(amounts, currentAmount, sum);
SetLastUniqueValue(amounts, currentAmount, sum);
}
highestUniqueAmonut = GetHighestUniqueAmonut(amount, amounts);
if (debug)
Print(banknotes, amount, amounts);
DeleteAmounts(amount, amounts);
return highestUniqueAmonut;
}
int main() {
std::cout << BreakAmountUniquely({7, 8, 9, 10}, 18, true) << "\n";
std::cout << BreakAmountUniquely({7, 8, 9, 10}, 19, true) << "\n";
}