File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 83
83
81|[ Search in Rotated Sorted Array II] ( ./0081-search-in-rotated-sorted-array-ii.js ) |Medium|
84
84
82|[ Remove Duplicates from Sorted List II] ( ./0082-remove-duplicates-from-sorted-list-ii.js ) |Medium|
85
85
83|[ Remove Duplicates from Sorted List] ( ./0083-remove-duplicates-from-sorted-list.js ) |Easy|
86
+ 85|[ Maximal Rectangle] ( ./0085-maximal-rectangle.js ) |Hard|
86
87
86|[ Partition List] ( ./0086-partition-list.js ) |Medium|
87
88
88|[ Merge Sorted Array] ( ./0088-merge-sorted-array.js ) |Easy|
88
89
89|[ Gray Code] ( ./0089-gray-code.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments