Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode 1727. Largest Submatrix With Rearrangements #202

Open
Woodyiiiiiii opened this issue Feb 1, 2023 · 0 comments
Open

Leetcode 1727. Largest Submatrix With Rearrangements #202

Woodyiiiiiii opened this issue Feb 1, 2023 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

Woodyiiiiiii commented Feb 1, 2023

这道题跟85. Maximal Rectangle有些类似。

根据题意,我们可以随意排序,组成我们想要的最大面积。所以,我们要根据贪心的思想,尽可能地要把多的同在一行的1集中在一起,这就想到排序;接着,在循环中用累加计算当前列的最大高度,再进行贪心排序。

class Solution {
    public int largestSubmatrix(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        int ans = 0;
        for (int i = 0; i < m; ++i) {
            List<Integer> dp = new ArrayList<>();
            for (int j = 0; j < n; ++j) {
                if (matrix[i][j] == 1 && i > 0) {
                    matrix[i][j] += matrix[i - 1][j];
                }
                dp.add(matrix[i][j]);
            }
            dp.sort(Collections.reverseOrder());
            for (int j = 0; j < n; ++j) {
                ans = Math.max(ans, dp.get(j) * (j + 1));
            }
        }
        return ans;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant