Ctrl K

Largest Frame of Ones

Medium
You get a rectangular table grid of 0s and 1s. Find the largest square whose border cells are all 1s (what is inside does not matter). Return the number of cells in that square, that is, its side length squared. If the table has no 1s at all, return 0. A single cell holding 1 counts as a square of side 1.
Example 1
grid=[[1, 1, 1, 0], [1, 0, 1, 1], [1, 1, 1, 1]] → 9

The 3 × 3 frame in the top-left corner is all 1s (the 0 inside does not matter), so it covers 9 cells.

Example 2
grid=[[0, 1], [1, 1]] → 1

A 2 × 2 square fails because of the 0 in the corner. Any single 1 works, giving 1 cell.

Example 3
grid=[[0, 0, 0]] → 0

There are no 1s at all, so the answer is 0.

Constraints
  • 1 ≤ number of rows ≤ 300
  • 1 ≤ number of columns ≤ 300
  • grid[i][j] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWarehouse Cart Routes MediumCheapest Corner-to-Corner Route MediumLargest Solid Square MediumBest Spot for the Turret MediumSteps to the Nearest Zero Medium
loading Python…
Input
grid=[[1, 1, 1, 0], [1, 0, 1, 1], [1, 1, 1, 1]]
Expected
9

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