-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
211 lines (187 loc) · 6.49 KB
/
App.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const createView = () => {
const container = document.getElementById("container");
container.innerHTML = "";
let resultHead = document.createElement("H3");
resultHead.id = "result"
container.appendChild(resultHead);
let inputContainer = document.createElement("DIV");
inputContainer.id = "input-container";
inputContainer.innerHTML = "Column heights: ";
let inputBox = document.createElement("INPUT");
inputBox.id = "input1";
inputContainer.appendChild(inputBox);
container.appendChild(inputContainer);
let flexContainer = document.createElement("DIV");
flexContainer.id = "flex-container";
container.appendChild(flexContainer);
}
(() => {
createView();
document.querySelector("body").addEventListener("keyup", event => {
if(event.keyCode == 46 && event.target === event.currentTarget) {
resetView(true);
}
});
document.getElementById('input1').addEventListener('keyup', (event) => {
if(event.keyCode == 13) {
resetView(false);
let numbers = event.target.value ? event.target.value.match(/\d+/g).map(Number) : [];
if(numbers.length > 30) {
console.log("HIBA: Maximum 30 oszlop engedélyezett.");
return;
} else if(Math.max(...numbers) > 30) {
console.log("HIBA: Maximum oszlopmagasság (30) túllépve.");
return;
}
let columns = [];
let totalWaterVolume = 0;
console.time("mySolution_outer");
let indexedMaxHeights = getIndexedMaxHeights(numbers);
console.time("mySolution_inner2");
numbers.forEach((element, index) => {
let localWaterLevel = Math.min(indexedMaxHeights.h1[index], indexedMaxHeights.h2[index]);
let localWaterHeight = localWaterLevel - element;
totalWaterVolume += localWaterHeight;
columns = [...columns, generateColumn(index, element, localWaterHeight),];
});
console.timeEnd("mySolution_inner2");
console.timeEnd("mySolution_outer");
draw(columns);
giveAnswer(totalWaterVolume);
}
});
})();
const getIndexedMaxHeights = (heightMap) => {
console.time("mySolution_inner1");
let ret = {
h1: [heightMap[0]],
h2: []
};
ret.h2[heightMap.length - 1] = heightMap[heightMap.length - 1];
for(let i = 1; i < heightMap.length; i++) {
ret.h1 = [...ret.h1, Math.max(ret.h1[i-1], heightMap[i])];
let correctedIndex = heightMap.length - (1 + i);
ret.h2[correctedIndex] = Math.max(heightMap[correctedIndex], ret.h2[correctedIndex+1]);
}
console.timeEnd("mySolution_inner1");
return ret;
};
const generateColumn = (colIndex, height, waterHeight) => {
let column = {
htmlElement: "DIV",
id: ("flex-item_" + colIndex),
cssClass: "flex-item",
height: height,
index: colIndex,
blocks: [
{
htmlElement: "DIV",
cssClass: "ground"
},
{
htmlElement: "DIV",
content: colIndex
}
]
};
// Generate blocks
for(let i = 0; i < height; i++) {
column.blocks = [
{
htmlElement: "DIV",
cssClass: "block"
},
...column.blocks,
]
}
//generate water
for(let i = 0; i < waterHeight; i++) {
column.blocks = [
{
htmlElement: "DIV",
cssClass: "water"
},
...column.blocks,
];
}
return column;
}
const draw = (drawable) => {
if(!Array.isArray(drawable)) {
console.log("Drawable is not an array.");
return;
}
const colContainer = document.getElementById("flex-container");
drawable.forEach(element => {
let colElement = document.createElement(element.htmlElement);
colElement.classList.add(element.cssClass);
colElement.id = element.id;
element.blocks.forEach(innerElement => {
let colBlock = document.createElement(innerElement.htmlElement);
if(innerElement.cssClass) colBlock.classList.add(innerElement.cssClass);
colBlock.innerHTML = innerElement.content ?? '';
colElement.appendChild(colBlock);
});
colContainer.appendChild(colElement);
});
}
const giveAnswer = (answer) => {
document.getElementById("result").innerHTML = answer;
}
const resetView = (hardReset) => {
document.getElementById("flex-container").innerHTML = "";
document.getElementById("result").innerHTML = "";
if(hardReset) document.getElementById("input1").value = "";
}
// Egyszerű (slice)
const getAnswer_simple_slice = (heightMap) => {
console.time("simple_slice");
let totalWater = 0;
let n = heightMap.length;
let waterHeights = [];
for(let i = 0; i < n; i++) {
// max(heightMap[1],...,heightMap[i])
leftMax = Math.max(...heightMap.slice(0, i+1));
// max(heightMap[i],...,heightMap[n])
rightMax = Math.max(...heightMap.slice(i, heightMap.length));
// aktuális víz magasság
waterHeight = Math.min(leftMax, rightMax) - heightMap[i];
waterHeights = [...waterHeights, waterHeight];
totalWater += waterHeight;
}
console.timeEnd("simple_slice");
return {
heights: waterHeights,
totalVolume: totalWater
};
}
// Egyszerű (reduce)
const getAnswer_simple_reduce = (heightMap) => {
console.time("simple_reduce");
let totalWater = 0;
let n = heightMap.length;
let waterHeights = [];
for(let i = 0; i < n; i++) {
// max(heightMap[1],...,heightMap[i])
leftMax = heightMap.reduce((accumulator, current, index, source) => {
if(index > i) return accumulator;
if(current > accumulator) return current;
else return accumulator;
});
// max(heightMap[i],...,heightMap[n])
rightMax = heightMap.reduce((accumulator, current, index, source) => {
if(index < i) return 0;
if(current > accumulator) return current;
else return accumulator;
});
// aktuális víz magasság
waterHeight = Math.min(leftMax, rightMax) - heightMap[i];
waterHeights = [...waterHeights, waterHeight];
totalWater += waterHeight;
}
console.timeEnd("simple_reduce");
return {
heights: waterHeights,
totalVolume: totalWater
};
}