All problems
0943MediumArrayBinary SearchDynamic ProgrammingMatrix

Two Equal Squares of Sound Panel

Tracked in this browser only
Write code

Trains the technique from

LeetCode 4016Maximum Area of Two Non-Overlapping Square Submatrices

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 panel is given as mat, each cell holding 0 or 1. A cell holding 1 is sound.

Find two squares of sound cells, both the same size, that do not overlap each other, choosing the largest size for which two such squares exist. Return the total area they cover, which is twice the square of that size, or 0 when no two squares of any size fit.

Examples

Example 1

Input
mat = [[1, 1, 1, 1], [1, 1, 1, 1]]
Output
8

Two squares of side two fit side by side in the four columns without overlapping, covering eight cells between them. No larger size fits at all on a panel two rows deep.

Example 2

Input
mat = [[1, 1]]
Output
2

Two single sound cells sit apart from each other, covering two cells.

Example 3

Input
mat = [[1]]
Output
0

There is only one sound cell, so no two squares fit.

Constraints

  • 1 <= mat.length <= 500
  • 1 <= mat[i].length <= 500
  • Every row of mat has the same length
  • mat[i][j] is either 0 or 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 max_area(mat: list[list[int]]) -> int:
Java
public int maxArea(int[][] mat)
September 7
Apply