16/32
Part 4 of 7 · Many sourcesLearn and do

One search from every source at once costs O(RC), whatever the number of sources

The other way to get this map runs one BFS per source and keeps the smaller count for each cell. It gives the same answer at SS times the cost for SS sources. On an open 200 by 200 grid, 40,000 cells, with sources at random spots:

SourcesOne search per sourceOne search from all
140,000 pops, 0.032 s40,000 pops, 0.033 s
10400,000 pops, 0.336 s40,000 pops, 0.032 s
502,000,000 pops, 1.659 s40,000 pops, 0.030 s

Both methods produced the same map in every row. From all sources at once it's O(RC)O(RC), whatever SS is.

In the NeetCode video at the end of this part, he counts fresh oranges down as they rot, and anything left at the end means -1. That's the same check as comparing the cells you reached with the cells that had to be reached, done with a counter.

In the editor, minutes_to_burn(grid) finds every fire F and counts the trees. Fill in its two missing lines: seed seen and queue with every fire, and return the answer.