-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path085.js
More file actions
68 lines (58 loc) · 1.42 KB
/
085.js
File metadata and controls
68 lines (58 loc) · 1.42 KB
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
/**
* @param {character[][]} matrix
* @return {number}
*/
var largestRectangleArea = function(heights) {
let stack = [];
let result = 0;
heights.forEach(height => {
if (stack.length === 0 || stack[stack.length - 1] <= height) {
stack.push(height);
} else {
let count = 0;
while (stack.length > 0 && stack[stack.length - 1] > height) {
++count;
const h = stack.pop();
if (h * count > result) result = h * count;
}
while (count >= 0) {
stack.push(height);
--count;
}
}
});
const length = stack.length;
for(let i = 0; i < length; ++i) {
const value = stack[i] * (length - i);
if (value > result) result = value;
}
return result;
};
var maximalRectangle = function(matrix) {
const rowCount = matrix.length;
if (rowCount === 0) return 0;
const colCount = matrix[0].length;
if (colCount === 0) return 0;
getHeight = (row, col) => {
let height = 0;
while(row >= 0) {
if (matrix[row][col] === "1") {
++height;
--row;
} else {
break;
}
}
return height;
}
let maxValue = 0;
for(let row = 0; row < rowCount; ++row) {
let heights = [];
for(let col = 0; col < colCount; ++col) {
heights.push(getHeight(row, col));
}
const largestArea = largestRectangleArea(heights);
if (largestArea > maxValue) maxValue = largestArea;
}
return maxValue;
};