Skip to content

Commit 3a978e5

Browse files
committed
Add solution #85
1 parent 65db578 commit 3a978e5

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
81|[Search in Rotated Sorted Array II](./0081-search-in-rotated-sorted-array-ii.js)|Medium|
8484
82|[Remove Duplicates from Sorted List II](./0082-remove-duplicates-from-sorted-list-ii.js)|Medium|
8585
83|[Remove Duplicates from Sorted List](./0083-remove-duplicates-from-sorted-list.js)|Easy|
86+
85|[Maximal Rectangle](./0085-maximal-rectangle.js)|Hard|
8687
86|[Partition List](./0086-partition-list.js)|Medium|
8788
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
8889
89|[Gray Code](./0089-gray-code.js)|Medium|

solutions/0085-maximal-rectangle.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 85. Maximal Rectangle
3+
* https://leetcode.com/problems/maximal-rectangle/
4+
* Difficulty: Hard
5+
*
6+
* Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle
7+
* containing only 1's and return its area.
8+
*/
9+
10+
/**
11+
* @param {character[][]} matrix
12+
* @return {number}
13+
*/
14+
var maximalRectangle = function(matrix) {
15+
if (!matrix?.length) return 0;
16+
let maxArea = 0;
17+
18+
for (let i = 0; i < matrix.length; i++) {
19+
for (let j = 0; j < matrix[0].length; j++) {
20+
if (matrix[i][j] === '1') {
21+
let minWidth = matrix[0].length;
22+
23+
for (let k = i; k < matrix.length && matrix[k][j] === '1'; k++) {
24+
let width = 0;
25+
26+
while (width < minWidth && j + width < matrix[0].length && matrix[k][j + width] === '1') {
27+
width++;
28+
}
29+
30+
minWidth = Math.min(minWidth, width);
31+
maxArea = Math.max(maxArea, minWidth * (k - i + 1));
32+
}
33+
}
34+
}
35+
}
36+
37+
return maxArea;
38+
};

0 commit comments

Comments
 (0)