All problems
1186MediumArrayMatrix

The Smallest Crop Around the Marks

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3195Find the Minimum Area to Cover All Ones I

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A scanned page is held as a grid of cells. sheet[i][j] is 1 when that cell carries a mark and 0 when it is blank, and at least one cell carries a mark.

You want to crop the page down to a rectangle of whole cells whose sides run along the rows and columns, keeping every marked cell inside the crop. Blank cells inside the crop are fine.

Return the smallest number of cells such a crop can hold.

Examples

Example 1

Input
sheet = [[1, 0], [0, 1]]
Output
4

The two marks sit on different rows and different columns, so the crop has to span both rows and both columns and takes in all four cells.

Example 2

Input
sheet = [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]
Output
4

The marks occupy rows 1 and 2 and columns 1 and 2, so a two by two crop holds them. The blank cell it picks up in the corner costs nothing.

Example 3

Input
sheet = [[0, 1, 0, 0, 0]]
Output
1

One mark on a single row, so the crop closes down to that one cell.

Constraints

  • 1 <= sheet.length <= 1000
  • 1 <= sheet[i].length <= 1000
  • 0 <= sheet[i][j] <= 1
  • at least one cell of sheet holds 1

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def minimum_area(sheet: list[list[int]]) -> int:
Java
public int minimumArea(int[][] sheet)
September 7
Apply