Problem 544328 · medium · Phase 05 Advanced Algorithms & Graphs

Largest Square Plot

dynamic programming · grid DP · maximal square

A land survey land marks each cell with 1 (buildable) or 0 (swamp). A builder wants the largest square block of cells that are all buildable. Return its area (side length squared), or 0 if no buildable cell exists.

Examples

Input:  land = [[1,1,0,1],
                [1,1,1,1],
                [0,1,1,1],
                [1,1,1,1]]
Output: 9
Explanation: rows 1-3, columns 1-3 form a 3x3 buildable square.

Input:  land = [[0,0],
                [0,0]]
Output: 0

Constraints

  • 1 <= rows, cols <= 100
  • land[i][j] is 0 or 1
  • Target complexity: O(rows * cols). Checking every candidate square explicitly is O((rows * cols) * min(rows, cols)**2) and too slow.

Goals

  • Define a state as 'largest square whose bottom-right corner is this cell'
  • Combine three neighbouring states with a minimum
Starting Python…