Talentd
FleetCode

1139. Largest 1-Bordered Square

Medium52.7% acceptance4 companies

Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.

Example 1:

Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
Output: 9

Example 2:

Input: grid = [[1,1,0,0]]
Output: 1

Constraints:

  • 1 <= grid.length <= 100
  • 1 <= grid[0].length <= 100
  • grid[i][j] is 0 or 1

Hints open one at a time

Hint 1
For each square, know how many ones are up, left, down, and right of this square. You can find it in O(N^2) using dynamic programming.
Hint 2
Now for each square ( O(N^3) ), we can evaluate whether that square is 1-bordered in O(1).
1:22:20
Still stuck?Read 5 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def largest1BorderedSquare(self, grid: List[List[int]]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[[1,1,1],[1,0,1],[1,1,1]]
Input
[[1,1,0,0]]
Your code is saved on this device.Solution