given a matrix with each element corresponding to height above sea level, determine what elements can reach both the pacific and atlantic ocean (pacific - north and west, atlantic - east and south).

Approach 1: Breadth-First Search

  • utilize two queues for each ocean for BFS
  • add outer elements already touching the oceans (borders of matrix)
  • process each queue separately in a normal BFS loop (while queue is not empty)
    • mark element as visited in corresponding ocean boolean matrix
    • for each direction
      • add to queue if unvisited, within bounds, and if value is greater (problem-specific condition)
  • traverse through visited matrices and add to list if element is true in both
  • return list

Reflection

  • Working backwards from goal states for traversal instead of starting at beginning index
  • utilizing BFS to explore multiple routes at once

Question: Is it required to use BFS?