Find Cells Whose Water Can Reach Both Oceans

Quick Overview

Practice finding grid cells whose water can reach both specified ocean boundaries. Preserve downhill or equal-height flow, four-direction adjacency, and complete coordinate coverage.

Find Cells Whose Water Can Reach Both Oceans

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Find Cells Whose Water Can Reach Both Oceans A rectangular island is represented by an `m` by `n` matrix `heights`. The Pacific Ocean touches its top and left edges. The Atlantic Ocean touches its bottom and right edges. From a cell, water may move north, south, east, or west to a neighboring cell of equal or lower height. From any boundary cell touching an ocean, it may flow directly into that ocean. Return the grid coordinates of all cells from which water can reach both oceans. Coordinates are zero-based pairs `[row,column]`. ### Function `vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights)` ### Example 1 ```text Input: heights = [[1,2],[4,3]] Output: [[0,1],[1,0],[1,1]] ``` The top-right and bottom-left cells directly touch both oceans. The bottom-right cell touches the Atlantic and can flow through the top-right cell to the Pacific. The top-left cell cannot flow uphill to reach the Atlantic. ### Example 2 ```text Input: heights = [[2,2,2],[2,2,2],[2,2,2]] Output: [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] ``` Every cell can flow across the equal-height plateau to either ocean. The examples list coordinates in row order. ### Constraints - `m == heights.length` and `n == heights[r].length`. - `1 <= m,n <= 200`. - `0 <= heights[r][c] <= 100000`. ### Clarifications The matrix is rectangular. Equal-height movement is allowed; diagonal movement is not. A cell may use different routes to the two oceans. Include every qualifying cell once, including boundary cells when they satisfy both reachability conditions. ### What a Strong Answer Covers - The exact ocean boundaries and the direction of permitted water flow. - Equal heights, interior cells, and single-row or single-column islands. - Why all and only qualifying coordinates are returned. - Correctness and resource bounds for the full grid size.

Overview: Practice finding grid cells whose water can reach both specified ocean boundaries. Preserve downhill or equal-height flow, four-direction adjacency, and complete coordinate coverage.

Amazon logo
Amazon
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Find Cells Whose Water Can Reach Both Oceans

A rectangular island is represented by an m by n matrix heights. The Pacific Ocean touches its top and left edges. The Atlantic Ocean touches its bottom and right edges.

From a cell, water may move north, south, east, or west to a neighboring cell of equal or lower height. From any boundary cell touching an ocean, it may flow directly into that ocean.

Return the grid coordinates of all cells from which water can reach both oceans. Coordinates are zero-based pairs [row,column].

Function

vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights)

Example 1

Input: heights = [[1,2],[4,3]]
Output: [[0,1],[1,0],[1,1]]

The top-right and bottom-left cells directly touch both oceans. The bottom-right cell touches the Atlantic and can flow through the top-right cell to the Pacific. The top-left cell cannot flow uphill to reach the Atlantic.

Example 2

Input: heights = [[2,2,2],[2,2,2],[2,2,2]]
Output: [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]

Every cell can flow across the equal-height plateau to either ocean. The examples list coordinates in row order.

Constraints

  • m == heights.length and n == heights[r].length .
  • 1 <= m,n <= 200 .
  • 0 <= heights[r][c] <= 100000 .

Clarifications

The matrix is rectangular. Equal-height movement is allowed; diagonal movement is not. A cell may use different routes to the two oceans. Include every qualifying cell once, including boundary cells when they satisfy both reachability conditions.

What a Strong Answer Covers Guidance

  • The exact ocean boundaries and the direction of permitted water flow.
  • Equal heights, interior cells, and single-row or single-column islands.
  • Why all and only qualifying coordinates are returned.
  • Correctness and resource bounds for the full grid size.