File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 1- # 1,503 LeetCode solutions in JavaScript
1+ # 1,504 LeetCode solutions in JavaScript
22
33[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
44
133113311723|[ Find Minimum Time to Finish All Jobs] ( ./solutions/1723-find-minimum-time-to-finish-all-jobs.js ) |Hard|
133213321725|[ Number Of Rectangles That Can Form The Largest Square] ( ./solutions/1725-number-of-rectangles-that-can-form-the-largest-square.js ) |Easy|
133313331726|[ Tuple with Same Product] ( ./solutions/1726-tuple-with-same-product.js ) |Medium|
1334+ 1727|[ Largest Submatrix With Rearrangements] ( ./solutions/1727-largest-submatrix-with-rearrangements.js ) |Medium|
133413351732|[ Find the Highest Altitude] ( ./solutions/1732-find-the-highest-altitude.js ) |Easy|
133513361748|[ Sum of Unique Elements] ( ./solutions/1748-sum-of-unique-elements.js ) |Easy|
133613371749|[ Maximum Absolute Sum of Any Subarray] ( ./solutions/1749-maximum-absolute-sum-of-any-subarray.js ) |Medium|
Original file line number Diff line number Diff line change 1+ /**
2+ * 1727. Largest Submatrix With Rearrangements
3+ * https://leetcode.com/problems/largest-submatrix-with-rearrangements/
4+ * Difficulty: Medium
5+ *
6+ * You are given a binary matrix matrix of size m x n, and you are allowed to rearrange the
7+ * columns of the matrix in any order.
8+ *
9+ * Return the area of the largest submatrix within matrix where every element of the submatrix
10+ * is 1 after reordering the columns optimally.
11+ */
12+
13+ /**
14+ * @param {number[][] } matrix
15+ * @return {number }
16+ */
17+ var largestSubmatrix = function ( matrix ) {
18+ const rows = matrix . length ;
19+ const cols = matrix [ 0 ] . length ;
20+ let result = 0 ;
21+
22+ for ( let row = 0 ; row < rows ; row ++ ) {
23+ for ( let col = 0 ; col < cols ; col ++ ) {
24+ if ( row > 0 && matrix [ row ] [ col ] === 1 ) {
25+ matrix [ row ] [ col ] += matrix [ row - 1 ] [ col ] ;
26+ }
27+ }
28+
29+ const heights = matrix [ row ] . slice ( ) . sort ( ( a , b ) => b - a ) ;
30+ for ( let i = 0 ; i < cols ; i ++ ) {
31+ if ( heights [ i ] === 0 ) break ;
32+ result = Math . max ( result , heights [ i ] * ( i + 1 ) ) ;
33+ }
34+ }
35+
36+ return result ;
37+ } ;
You can’t perform that action at this time.
0 commit comments