934. Shortest Bridge

find two island's shortest dist (by flipped)

idea: use DFS mark first island to 2 (1 to 2),

then for second island (value is 1), do BFS, until find 2 .=> it's done!

DFS or BFS are all O(mn), so O(mn)

time : O(m*n)

space: O(m*n)

flood fill

use visited[][]

Last updated

Was this helpful?