Ctrl K

Biggest Block After a Column Shuffle

Medium
You are given a table board of zeros and ones. You may rearrange its columns in any order (each column moves as a whole; rows stay in place). After rearranging, look for a rectangular block of adjacent cells made entirely of ones. Return the largest area such a block can have under the best column order. If there are no ones, the answer is 0.
Example 1
board=[[1, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 1]] → 6

Columns 0 and 3 are all ones. Put them side by side to get a 3×2 block with area 6.

Example 2
board=[[0, 1, 0, 1, 1]] → 3

The three ones can be gathered side by side: a 1×3 block.

Example 3
board=[[1, 0], [0, 1]] → 1

However the columns are ordered, the ones sit in different rows, so the best block is a single cell.

Constraints
  • 1 ≤ rows × columns ≤ 10⁵
  • All rows have the same length
  • board[i][j] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBest Picks From Capped Shelves MediumSpend the Most Across Shops HardBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low Medium
loading Python…
Input
board=[[1, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 1]]
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 86 tests on the server, hidden ones included.