From each cell, do two DFS/BFS: one to check if it reaches Pacific, one for Atlantic.
For each of cells, you might explore the entire grid twice. Total: .
Can you reverse the problem?
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
The trap
From each cell, do two DFS/BFS: one to check if it reaches Pacific, one for Atlantic.
For each of cells, you might explore the entire grid twice. Total: .
Can you reverse the problem?