-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10.js
42 lines (32 loc) · 1.04 KB
/
10.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
var largestRectangleArea = function(heights) {
let n = heights.length;
let max = 0;
let stack = [];
for(let i = 0; i <= n; i++) {
let step = 0;
let bar = heights[i] || 0;
while(stack.length && stack[stack.length-1][1] >= bar) {
let [w, h] = stack.pop();
step += w;
max = Math.max(max, step * h);
}
stack.push([step+1, bar]);
}
return max;
};
let getResult = (data) => {
let parse_ints = str => {
return str.split(' ').filter(val => val != '').map(val => parseInt(val.trim()));
}
let n = parseInt(data[0]);
let arr = parse_ints(data[1]);
return largestRectangleArea(arr);
}
const { count } = require('console');
//console.log('start: ' + Date.now());
const fs = require('fs');
let fileContent = fs.readFileSync("input.txt", "utf8");
const data = fileContent.toString().trim().split("\n");
const result = getResult(data);
fs.writeFileSync("output.txt", result.toString());
//console.log('end: ' + Date.now());