Skip to content

Commit a4b37e0

Browse files
committed
#Largest Rectangle
1 parent 27d6da6 commit a4b37e0

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

largest-rectangle.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
process.stdin.resume();
6+
process.stdin.setEncoding('utf-8');
7+
8+
let inputString = '';
9+
let currentLine = 0;
10+
11+
process.stdin.on('data', inputStdin => {
12+
inputString += inputStdin;
13+
});
14+
15+
process.stdin.on('end', _ => {
16+
inputString = inputString.replace(/\s*$/, '')
17+
.split('\n')
18+
.map(str => str.replace(/\s*$/, ''));
19+
20+
main();
21+
});
22+
23+
function readLine() {
24+
return inputString[currentLine++];
25+
}
26+
27+
// Complete the largestRectangle function below.
28+
function largestRectangle(h) {
29+
let max = Number.MIN_VALUE;
30+
for (var i = 0; i < h.length; i++) {
31+
let l = i - 1;
32+
let r = i + 1;
33+
let m = 1;
34+
while (l > -1) {
35+
if (h[l] >= h[i]) {
36+
// console.log("left", l)
37+
m++;
38+
}
39+
else {
40+
break;
41+
}
42+
l--;
43+
}
44+
while (h.length > r) {
45+
// console.log("right", r)
46+
if (h[r] >= h[i]) {
47+
m++;
48+
}
49+
else {
50+
break;
51+
}
52+
r++;
53+
}
54+
console.log("mcount", m, h[i])
55+
if (m * h[i] > max) {
56+
max = m * h[i];
57+
}
58+
m = 1;
59+
}
60+
return max;
61+
62+
}
63+
64+
function main() {
65+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
66+
67+
const n = parseInt(readLine(), 10);
68+
69+
const h = readLine().split(' ').map(hTemp => parseInt(hTemp, 10));
70+
71+
let result = largestRectangle(h);
72+
73+
ws.write(result + "\n");
74+
75+
ws.end();
76+
}

0 commit comments

Comments
 (0)