-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1727-largest-submatrix-with-rearrangements.js
More file actions
49 lines (45 loc) · 1.32 KB
/
1727-largest-submatrix-with-rearrangements.js
File metadata and controls
49 lines (45 loc) · 1.32 KB
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
39
40
41
42
43
44
45
46
47
48
49
/**
* Largest Submatrix With Rearrangements
* Time Complexity: O(rows * cols * log(cols))
* Space Complexity: O(cols)
*/
var largestSubmatrix = function (matrix) {
const totalRows = matrix.length;
const totalColumns = matrix[0].length;
let maximumAchievedArea = 0;
for (
let currentRowIndex = 0;
currentRowIndex < totalRows;
currentRowIndex++
) {
for (
let currentColumnIndex = 0;
currentColumnIndex < totalColumns;
currentColumnIndex++
) {
if (
currentRowIndex > 0 &&
matrix[currentRowIndex][currentColumnIndex] === 1
) {
matrix[currentRowIndex][currentColumnIndex] +=
matrix[currentRowIndex - 1][currentColumnIndex];
}
}
const currentColumnHeights = matrix[currentRowIndex].slice();
currentColumnHeights.sort((heightA, heightB) => heightB - heightA);
for (
let sortedHeightIndex = 0;
sortedHeightIndex < totalColumns;
sortedHeightIndex++
) {
const heightValue = currentColumnHeights[sortedHeightIndex];
if (heightValue === 0) {
break;
}
const currentSubmatrixWidth = sortedHeightIndex + 1;
const calculatedArea = heightValue * currentSubmatrixWidth;
maximumAchievedArea = Math.max(maximumAchievedArea, calculatedArea);
}
}
return maximumAchievedArea;
};