-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question3.js
103 lines (96 loc) · 3.75 KB
/
Question3.js
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
// Question 3 -- changePossibilities(amount,amount): Your quirky boss collects rare, old coins. They found out you're a programmer and asked you to solve something they've been wondering for a long time.
// Write a function that, given an amount of money and an array of coin denominations, computes the number of ways to make the amount of money with coins of the available denominations.
// Example: for amount=4 (4¢) and denominations=[1,2,3] (1¢, 2¢ and 3¢), your program would output 4—the number of ways to make 4¢ with those denominations:
// 1¢, 1¢, 1¢, 1¢
// 1¢, 1¢, 2¢
// 1¢, 3¢
// 2¢, 2¢
function changePossibilities (amount, denominations) {
let times = amount * denominations.length;
let possibilities = {};
while (times > 0) { // this section account for numbers in the denominations array that where repeating number can add up to the amount
for (let i = 0; i < denominations.length; i++) {
let temp = [];
let count = 0;
let current = denominations[i];
count += current;
temp.push(current);
for ( let j = 0; j < denominations.length; j++) {
let next = denominations[j];
if (count + next === amount) {
temp.push(next);
temp.sort((a,b) => a-b);
if (!possibilities[temp] ) {
possibilities[temp] = temp;
}
}
}
}
times --;
}
let check = Object.values(possibilities);
if (check.length === 0) {
times = amount * denominations.length
while (times > 0) { // this part of thee section accounts for congruent numbers in the denominations that can add up to amount
let count = 0;
let temp = [];
let tempDenom = denominations.slice()
for (let i = 0; i < tempDenom.length; i++) {
i = 0;
let current = tempDenom[i];
count += current;
temp.push(current);
for ( let j = i+1; j < tempDenom.length; j++) {
let next = tempDenom[j];
temp.push(next);
count += next;
if (count === amount) {
temp.sort((a,b) => a-b);
if (!possibilities[temp] ) {
possibilities[temp] = temp;
}
}
if ( count < amount && j === tempDenom.length-1 ) {
count = 0;
i = 0;
temp.push(next);
tempDenom.shift();
} else if (count >= amount) {
temp = [];
count = 0;
tempDenom.shift()
i = 0;
}
}
}
times --;
}
}
let values = Object.values(possibilities);
let temp = [];
// this section accounts for combinations already found and the combinations possible in the current possibilites
for (let i = 0; i < values.length; i++) {
for(let j = 0; j < denominations.length; j++) {
let value = values[i][i];
if (amount % denominations[j] === 0 ) {
let array = new Array(amount / denominations[j])
array.fill(denominations[j], 0);
if (!possibilities[array] ) {
possibilities[array] = array;
}
}
if (value % denominations[j] === 0) {
let spread = value / denominations[0];
if (denominations[j]* spread + value === amount) {
let array = new Array(spread);
array.fill(denominations[j], 0,spread);
array.push(value);
if (!possibilities[array] ) {
possibilities[array] = array;
}
}
}
}
}
return Object.values(possibilities).sort((a,b) => a-b);
}