Skip to content
LC-1727 Medium LeetCode

1727. Largest Submatrix With Rearrangements

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 75% Topics: Array, Greedy, Sorting, Matrix
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(m * nlogn)
# Space: O(1)

class Solution(object):
    def largestSubmatrix(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: int
        """
        for c in xrange(len(matrix[0])):
            h = 0
            for r in xrange(len(matrix)):
                h = h+1 if matrix[r][c] == 1 else 0
                matrix[r][c] = h
        result = 0
        for row in matrix:
            row.sort()
            for c in xrange(len(row)):
                result = max(result, (len(row)-c) * row[c])
        return result

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions