↓Skip to main content
  1. LeetCode/

LeetCode 1727: Largest Submatrix With Rearrangements

·2 mins· ·
LeetCode Medium Greedy Sorting Matrix
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-06-23

  • Submissions: Accepted (first try)

Approach
#

Fix the submatrix’s bottom edge at row i. Each column has h[j] consecutive ones going up (the same heights as “largest rectangle in a histogram”).

Columns can be reordered, so put the tall ones together: sort heights descending; taking the first j + 1 columns, the height is limited by the (j + 1)-th tallest, giving area sorted_h[j] × (j + 1). Take the maximum over every row and j.

After reordering, column positions no longer matter, so no monotonic stack is needed; sorting is enough (Greedy Template 1).

My Solution
#

class Solution:
    def largestSubmatrix(self, matrix: List[List[int]]) -> int:
        n = len(matrix[0])
        h = [0] * n
        best = 0
        for row in matrix:
            for j in range(n):
                h[j] = h[j] + 1 if row[j] else 0
            for j, height in enumerate(sorted(h, reverse=True)):
                best = max(best, height * (j + 1))
        return best

I originally accumulated the heights inside matrix; here a separate h avoids modifying the input.

Complexity
#

Time O(m · n log n), space O(n).

Mistakes to Avoid
#

  • A 0 resets the height; it does not carry over.
  • Sort a copy of the heights, not h itself (the next row needs each column’s own height).

Interview Explanation
#

I fix the bottom edge and compute how many consecutive ones each column has above it. Columns can be reordered, so I sort heights descending; using the first j + 1 columns the height is the (j + 1)-th tallest, and the area is that times j + 1. I do this for every row and keep the maximum. Time O(m · n log n).

Related

LeetCode 3961: Maximize Sum of Device Ratings
·2 mins
LeetCode Medium Greedy Sorting Matrix
LeetCode 3961 note: every device but one donates its smallest unit to the same device; that device’s rating becomes the global minimum, so pick the one with the smallest second-smallest value
LeetCode 1833: Maximum Ice Cream Bars
·2 mins
LeetCode Medium Greedy Sorting Counting-Sort
LeetCode 1833 note: buy the cheapest first; the problem asks for counting sort, and with values up to 10^5 each price buys min(count, coins // price)
LeetCode 3555: Smallest Subarray to Sort in Every Sliding Window
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 3555 note: every length-k window is one instance of LeetCode 581; with n only 1000, two O(k) scans per window are enough
LeetCode 581: Shortest Unsorted Continuous Subarray
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 581 note: scanning left to right with a running max, the last index below it is the right boundary; scanning right to left with a running min, the last index above it is the left boundary
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 min
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 1877 note: after sorting, pair the smallest with the largest, the second smallest with the second largest, and take the largest pair sum with converging pointers
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k