Advertisement

Pacific Atlantic Water Flow - LeetCode 417 Solution

Pacific Atlantic Water Flow - Complete Solution Guide

Pacific Atlantic Water Flow is LeetCode problem 417, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean . The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c) . The island receives a lot of rain, and the rain water can flow to neighboring cells d

Detailed Explanation

The problem asks us to find all cells in a given `m x n` matrix (representing an island's heights) from which water can flow to both the Pacific and Atlantic oceans. The Pacific Ocean touches the top and left edges, and the Atlantic Ocean touches the bottom and right edges. Water can only flow from a cell to its neighboring cells (north, south, east, west) if the neighboring cell's height is less than or equal to the current cell's height. We need to return a list of coordinates `[r, c]` that can reach both oceans.

Solution Approach

The solution uses Breadth-First Search (BFS) to find all cells reachable from the Pacific and Atlantic oceans, respectively. It initializes two sets, `pacific_reachable` and `atlantic_reachable`, to keep track of the cells reachable from each ocean. It starts BFS from all edge cells adjacent to the Pacific and Atlantic. The BFS explores adjacent cells only if their height is greater than or equal to the current cell's height. Finally, it finds the intersection of the two sets to identify cells reachable from both oceans.

Step-by-Step Algorithm

  1. Step 1: Create two sets, `pacific_reachable` and `atlantic_reachable`, to store the coordinates reachable from each ocean.
  2. Step 2: Identify the starting cells for BFS from the Pacific Ocean (cells on the top and left edges) and the Atlantic Ocean (cells on the bottom and right edges).
  3. Step 3: Perform BFS from the Pacific starting cells. In each step, explore the adjacent cells (up, down, left, right). Add the adjacent cell to `pacific_reachable` if it's within bounds, not already visited, and its height is greater than or equal to the current cell's height.
  4. Step 4: Perform BFS from the Atlantic starting cells, similar to Step 3, but add reachable cells to `atlantic_reachable`.
  5. Step 5: Find the intersection of `pacific_reachable` and `atlantic_reachable`. The cells in this intersection are reachable from both oceans.
  6. Step 6: Convert the coordinates in the intersection set into a list of lists and return the result.

Key Insights

  • Insight 1: Instead of starting from each cell and trying to reach both oceans (which would be inefficient), it's much more efficient to start from the oceans and find all cells reachable from them.
  • Insight 2: We can use either Depth-First Search (DFS) or Breadth-First Search (BFS) to traverse the grid from the edges (oceans) and mark the cells that are reachable.
  • Insight 3: A crucial observation is that water flows from higher or equal height cells to lower height cells or to the ocean. This means we should explore the grid starting from the ocean boundaries and moving to the inland cells with heights equal to or greater than the current cell.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Topics

This problem involves: Array, Depth-First Search, Breadth-First Search, Matrix.

Companies

Asked at: Coupang, Nutanix, ServiceNow.