Editor

Largest Square Solar Panel

#351 · Grids and matrices · Advanced · 120 XP · about 25 min

A roof is mapped as a list of strings of equal length, one per row: '1' is a free tile and '0' is a tile taken by a chimney, a window or a vent. A solar panel must be a square of free tiles, with its sides along the rows and columns.

Write largest_square(roof) that returns the area (the number of tiles) of the largest square made only of '1' tiles, or 0 if there is no free tile.

Examples

largest_square(['11100',
                '11110',
                '11110',
                '01111'])   # returns 9
largest_square(['0110',
                '1111',
                '0110'])    # returns 4
largest_square(['000'])     # returns 0

The first roof has two 3 by 3 squares of free tiles (one at the top left, one at the bottom right) but no 4 by 4 square.

Constraints

  • 1 to 400 rows and 1 to 400 columns.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.