Test the edges, then check the fast version against the slow one
Before you call the spread done, test it the way the interviewer will.
Predict
The forest is ["FF", "F#"]: three fires and a rock. What should minutes_to_burn_k return?
4. Test the edges.
["FF", "F#"] returns 0: nothing is left to burn.
["...", "..."] returns -1: there's no fire.
["F..", "..."] with k = 2 returns -1: one fire can never give a tree two burning neighbors.
Then check the fast version against the slow one. On 3,000 random grids up to 6 by 6, each with k from 1 to 4, the two agreed in all 12,000 cases.
When fires never go out, the whole spread is O(RC)
5. Give the cost. Each cell is fresh at most once. Each tree's heat goes up at most four times, once per neighbor that catches. So the whole spread is O(RC) time and memory however many minutes it takes, where the rescan is O(RC) per minute. On the 60 by 60 forest with one corner fire: 3.0 ms against 0.169 s, the middle of five runs each.
The practice problem's first two levels are this spread, with infected cells for fires and immune cells for rocks. Its later levels add recovery and death. That's where the schedule and the two kinds of change in one minute come in. They also break the rule from the last step's worked example that a count changes only when a neighbor catches: a cell that recovers or dies stops counting, so each of its neighbors' heat comes down by one.
itertools.product(".#F", repeat=3) gives every way to fill 3 cells with those characters: ('.', '.', '.'), ('.', '.', '#') and so on, 27 in all. In the editor, write all_grids(R, C): every grid of that size made of ., # and F. Then write first_disagreement(fast, slow, cases): it runs two versions, such as minutes_to_burn_k and minutes_by_rescan, on each (grid, k) and returns the first case where they differ.
Pick an answer above to see the rest
Instructions
1
Write all_grids(R, C): every grid of R rows and C columns made of ., # and F, each grid a list of strings.
2
Write first_disagreement(fast, slow, cases): the first (grid, k) in cases where fast(grid, k) and slow(grid, k) differ, or None.