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 times the cost for sources. On an open 200 by 200 grid, 40,000 cells, with sources at random spots:
| Sources | One search per source | One search from all |
|---|---|---|
| 1 | 40,000 pops, 0.032 s | 40,000 pops, 0.033 s |
| 10 | 400,000 pops, 0.336 s | 40,000 pops, 0.032 s |
| 50 | 2,000,000 pops, 1.659 s | 40,000 pops, 0.030 s |
Both methods produced the same map in every row. From all sources at once it's , whatever 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.