forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0085-maximal-rectangle.js
38 lines (32 loc) · 963 Bytes
/
0085-maximal-rectangle.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
/**
* 85. Maximal Rectangle
* https://leetcode.com/problems/maximal-rectangle/
* Difficulty: Hard
*
* Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle
* containing only 1's and return its area.
*/
/**
* @param {character[][]} matrix
* @return {number}
*/
var maximalRectangle = function(matrix) {
if (!matrix?.length) return 0;
let maxArea = 0;
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] === '1') {
let minWidth = matrix[0].length;
for (let k = i; k < matrix.length && matrix[k][j] === '1'; k++) {
let width = 0;
while (width < minWidth && j + width < matrix[0].length && matrix[k][j + width] === '1') {
width++;
}
minWidth = Math.min(minWidth, width);
maxArea = Math.max(maxArea, minWidth * (k - i + 1));
}
}
}
}
return maxArea;
};