1727. Largest Submatrix With Rearrangements
You are given a binary matrix matrix of size m x n, and you are allowed to rearrange the columns of the matrix in any order.
Return the area of the largest submatrix within matrix where every element of the submatrix is 1 after reordering the columns optimally.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public: int largestSubmatrix(vector<vector<int>>& matrix) { int i, j, ans = 0, n = matrix.size(), m = matrix[0].size(); for(i = 0; i < m; i++) for(j = 1; j < n; j++) if(matrix[j][i]) matrix[j][i] = matrix[j-1][i] + matrix[j][i];
for(i = 0; i < n; i++) { sort(matrix[i].begin(), matrix[i].end(), greater<int>()); for (j = 0; j < m; j++) ans = max(ans, (j + 1) * matrix[i][j]); } return ans; } };
|