Pacific Atlantic Water Flow Problem

Pacific Atlantic Water Flow Problem — ExecCode Medium DSA Practice

Solve the Pacific Atlantic Water Flow problem on ExecCode. Free online medium DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. Pacific touches the left and top edges; Atlantic touches the right and bottom edges. Rain can flow to neighboring cells in four directions if the neighbor's height is less than or equal to the current cell's height. Return a list of grid coordinates where rainwater can flow to both the Pacific and Atlantic oceans.

Examples

Input heights = [[1, 2, 2, 3, 5], [3, 2, 3, 4, 4], [2, 4, 5, 3, 1], [6, 7, 1, 4, 5]]; Output [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1]]. Input heights = [[1]]; Output [[0, 0]]. Input heights = [[1, 1]]; Output [[0, 0], [0, 1]]

Constraints

m == heights.length n == heights[r].length 1 ≤ m, n ≤ 200 0 ≤ heights[r][c] ≤ 10⁵

Practice Pacific Atlantic Water Flow free on ExecCode. Browse DSA problems, topic map, and placement guides.