Skip to content

Commit ebe1306

Browse files
committed
Add solution #1727
1 parent 6bd096b commit ebe1306

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,503 LeetCode solutions in JavaScript
1+
# 1,504 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1331,6 +1331,7 @@
13311331
1723|[Find Minimum Time to Finish All Jobs](./solutions/1723-find-minimum-time-to-finish-all-jobs.js)|Hard|
13321332
1725|[Number Of Rectangles That Can Form The Largest Square](./solutions/1725-number-of-rectangles-that-can-form-the-largest-square.js)|Easy|
13331333
1726|[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|
13341335
1732|[Find the Highest Altitude](./solutions/1732-find-the-highest-altitude.js)|Easy|
13351336
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
13361337
1749|[Maximum Absolute Sum of Any Subarray](./solutions/1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy