-
Notifications
You must be signed in to change notification settings - Fork 0
/
0085-MaximalRectangle.js
56 lines (46 loc) · 1.59 KB
/
0085-MaximalRectangle.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
//-----------------------------------------------------------------------------
// Runtime: 88ms
// Memory Usage: 39.6 MB
// Link: https://leetcode.com/submissions/detail/385829838/
//-----------------------------------------------------------------------------
var solution = function() {
'use strict';
/**
* @param {character[][]} matrix
* @return {number}
*/
var maximalRectangle = function(matrix) {
if (matrix.length === 0) {
return 0;
}
let N = matrix.length, M = matrix[0].length;
let height = Array(M).fill(0);
let maxArea = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
height[j] = matrix[i][j] === '1' ? height[j] + 1 : 0;
}
maxArea = Math.max(maxArea, ComputeLargestRectangle(height));
}
return maxArea;
};
var ComputeLargestRectangle = function(height) {
let stack = [];
stack.push(-1);
let maxArea = 0;
for (let i = 0; i < height.length; i++) {
while (stack[stack.length - 1] !== -1 && height[stack[stack.length - 1]] >= height[i]) {
maxArea = Math.max(maxArea, height[stack.pop()] * (i - stack[stack.length - 1] - 1));
}
stack.push(i);
}
while (stack[stack.length - 1] !== -1) {
maxArea = Math.max(maxArea, height[stack.pop()] * (height.length - stack[stack.length - 1] - 1));
}
return maxArea;
};
return {
maximalRectangle: maximalRectangle
};
};
module.exports = solution();