-
Notifications
You must be signed in to change notification settings - Fork 2
/
largestRectangle.js
executable file
·85 lines (77 loc) · 2.1 KB
/
largestRectangle.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
/**
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1,
find the area of largest rectangle in the histogram.
For example, given height = [2,1,5,6,2,3], return 10.
*/
var largestRectangle=function largestRectangle(height) {
'use strict';
if (height===null || height.length===0) {
return 0;
}
var stack=[];
var max=0;
var i=0;
while (i<height.length) {
// push index to stack if current height is larger than previous
if (stack.length===0) {
stack.push(i);
i++;
}
else if (height[i]>=height[stack[stack.length-1]]) {
stack.push(i);
i++;
}
else {
// calculate max value when the current height is less than the previous one
var elem=stack.pop();
var h=height[elem];
var w;
if (stack.length===0) {
w=i;
}
else {
w=i-stack[stack.length-1]-1;
}
max=Math.max(h*w,max);
}
}
while (stack.length!==0) {
var elem=stack.pop();
var h=height[elem];
var w;
if (stack.length===0) {
w=i;
}
else {
w=i-stack[stack.length-1]-1;
}
max=Math.max(h*w,max);
}
return max;
};
var largestRect2=function largestRect2(height){
var max=0;
if (height===null || height.length===0) {
return 0;
}
var i=0;
while (i<height.length) {
var h=height[i];
max=Math.max(h*1, max);
var j=i-1;
while (j>=0) {
if (height[j]<h) {
h=height[j];
max = Math.max(height[j] * (i - j + 1), max);
}
else {
max = Math.max(h * (i - j + 1), max);
}
j--;
}
i++;
}
return max;
};
console.log(largestRect2([2,1,5,6,2,3]));
console.log(largestRect2([2,3,10,11,7]));