-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathcode.js
50 lines (37 loc) · 1.21 KB
/
code.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
const { Tracer, Array1DTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
const tracer = new Array1DTracer('Input array');
const areaTracer = new Array1DTracer('Max area calculation');
const logger = new LogTracer();
Layout.setRoot(new VerticalLayout([tracer, areaTracer, logger]));
function maxArea(height) {
tracer.set(height)
let left = 0;
let right = height.length - 1;
let maxArea = 0;
while (left < right) {
tracer.select(left);
tracer.select(right);
Tracer.delay();
logger.println(`Checking area between ${left} and ${right}`);
const h = Math.min(height[left], height[right]);
areaTracer.patch(0, h)
Tracer.delay();
const w = right - left;
areaTracer.patch(1, h)
Tracer.delay();
const area = h * w;
maxArea = Math.max(maxArea, area);
areaTracer.patch(2, maxArea)
Tracer.delay();
tracer.deselect(left, right)
Tracer.delay();
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}
const height1 = [1,8,6,2,5,4,8,3,7];
console.log(maxArea(height1)); // Output: 49