Problem 302543 · hard · Phase 03 Linear Management & Searching

Largest Square of Working Panels

matrix · dynamic programming · grids

A solar farm is an m x n grid panels of 1 (working) and 0 (broken). Maintenance wants to know the area of the largest square block consisting only of working panels. Return that area (0 if no panel works).

Examples

Input:  panels = [[1, 0, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1]]
Output: 4
Explanation: several 2 x 2 blocks of ones exist, but no 3 x 3 block.

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

Constraints

  • 1 <= m, n <= 300
  • Target: O(m * n) time; O(n) extra space is possible.

Goals

  • Define a subproblem per cell: the largest square ending at that cell
  • Combine three neighbouring subproblems with min
  • Return the area, not the side length
Starting Python…