Pick the fix
Why start the searches from the coast edges instead of from each cell?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Each coast search visits every cell at most once, so the work is shared
Right: two O(R·C) passes replace one search per cell.
Reversing the flow lets one search answer the question for every cell at once.
Why not the others?
- B Coast cells are the highest. Heights at the edges can be anything.
- C Rain cannot flow uphill. That is why the reversed search climbs instead of descends.