Loading
Loading
An installer surveys a flat roof divided into equal tiles. A 1 marks a tile that can hold a panel; a 0 marks a vent, skylight or chimney. Panels are sold only as squares, and a panel must sit entirely on usable tiles.
Return the area, in tiles, of the largest square panel that fits. If no tile is usable, return 0.
roof = [[1, 1, 1, 0], [1, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1]]row 0: 1 1 1 0squares of side 1row 1: 1 2 2 12 = 1 + min(up, left, up-left)row 2: 1 2 3 2a 3 × 3 square ends at (2, 2)row 3: 0 1 2 3another 3 × 3 ends at (3, 3)largest side 3area 99Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 1 | 1 | 1 | 0 |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 |
| 2 | 1 | 1 | 1 | 1 |
| 3 | 0 | 1 | 1 | 1 |
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(rows × cols) time · O(rows × cols) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 1 | 1 | 1 | 0 |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 |
| 2 | 1 | 1 | 1 | 1 |
| 3 | 0 | 1 | 1 | 1 |
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Rows 0-2, columns 0-2 form a 3 × 3 block of usable tiles.
Example 2
| 0 | 0 | 1 |
|---|---|---|
| 1 | 1 | 0 |
No 2 × 2 block is fully usable, so the best panel is one tile.