DYNP-P5

The largest free square

Practice 5 of 9Dynamic programming9 tests

The problem

A railway yard is drawn as a grid: rows is a list of strings of the same length, where "." is a free cell and "#" is a cell with track on it. Return the side length of the largest square made only of free cells. The square’s sides run along the grid.

largest_square(["#..#.",
                ".....",
                ".#...",
                ".#..."])    # 3
largest_square(["##", "##"])  # 0
largest_square([])            # 0

Limits

  • Up to 700 rows of 700 cells. Aim for one pass over the cells, a few steps each.

Builds on DYNP-5 Grids and 2-D DP.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

9 tests, not run yet
Test results
#TestResultReads · time
1The exampleNot run
2All trackNot run
3One free cellNot run
4No rowsNot run
5One rowNot run
6A long strip is not a squareNot run
7In the bottom-right cornerNot run
8700 × 700, all freeNot run
9700 × 700 yardNot run