-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-0-3-memory-reallocation.js
106 lines (79 loc) · 2.98 KB
/
5-0-3-memory-reallocation.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
104
105
106
function memAlloc(banks) {
var allPreviousConfigs = [];
var dict = [];
var highestValue;
var indexOfHighestValue;
function findHighestValue(banks) {
highestValue = Math.max.apply(null, banks);
console.log("Highest Vavlue: ", highestValue);
indexOfHighestValue = banks.indexOf(highestValue);
}
function redistributeHighestValue(banks, highestValue, indexOfHighestValue) {
banks[indexOfHighestValue] = 0;
if (indexOfHighestValue == banks.length - 1) {
var indexOfNextElement = 0;
} else {
var indexOfNextElement = indexOfHighestValue + 1;
}
while (highestValue > 0) {
banks[indexOfNextElement] = banks[indexOfNextElement] + 1;
if (indexOfNextElement == banks.length - 1) {
indexOfNextElement = 0;
} else {
indexOfNextElement += 1;
}
highestValue -= 1;
}
return banks;
}
function checkPreviousConfigs(banks, allPreviousConfigs) {
var foundSameConfig = false;
var currentConfig = "";
for (var i = 0; i < banks.length; i++) {
currentConfig = currentConfig + banks[i].toString() + "-";
}
dict.push({
key: currentConfig,
value: banks
});
for (var z = 0; z < allPreviousConfigs.length; z++) {
if (currentConfig == allPreviousConfigs[z]) {
//console.log("There is a match: " + allPreviousConfigs[z]);
foundSameConfig = true;
break;
}
}
addToPreviousConfigs(currentConfig, allPreviousConfigs);
return foundSameConfig;
}
function addToPreviousConfigs (currentConfig, allPreviousConfigs) {
allPreviousConfigs.push(currentConfig);
}
//************************************************
//* START *
//************************************************
var counter = 0;
var foundTheSameConfig = false;
checkPreviousConfigs(banks, allPreviousConfigs);
while (foundTheSameConfig == false) {
findHighestValue(banks);
redistributeHighestValue(banks, highestValue, indexOfHighestValue);
foundTheSameConfig = checkPreviousConfigs(banks, allPreviousConfigs);
counter += 1;
}
return counter;
}
var banks = [0,2,7,0]; //result 5, OK;
// var banks = [5, 1, 10, 0, 1, 7, 13, 14, 3, 12, 8, 10, 7, 12, 0, 600] // 70 - OK time: 125627,473617510
// var banks = [53, 21, 10, 0, 1, 7, 13, 14, 3, 12, 8, 10, 7, 12, 0, 60];// 316 OK;
// var banks = [14, 21, 10, 0, 1, 7, 0, 14, 3, 12, 8, 10, 17, 12, 0, 19] // 826 OK;
//
//var banks = [5, 1, 10, 0, 1, 7, 13, 14, 3, 12, 8, 10, 7, 12, 0, 6] // 5042; wychodzi 1671 ?? SOLVED!!
// label: 189.669ms // dla dwóch funkcji - 200.000 ms
// after Math.max usage - 250.000 ms
// var banks = [17, 17, 3, 5, 1, 10, 6, 2, 0, 12, 8, 11, 16, 2, 1, 6] // 158378 OK!!!!!!
console.time('label');
var result = memAlloc(banks);
console.log("result is " + result);
console.log("time: " + process.hrtime());
console.timeEnd('label');