Pacific Atlantic Water Flow
Problem Description
Given a 2D matrix
representing an island and the ocean, find the number of cells with water flowing from the Pacific Ocean to the Atlantic Ocean.
Each cell is marked as either the ocean (
), the Pacific Ocean (
), the Atlantic Ocean (
), or the land (
).
For water to flow from the Pacific Ocean to the Atlantic Ocean, cells must be connected horizontally or vertically. We are interested in counting the number of such cells.
Rules and Constraints
- The input is a 2D array of integers, where each integer is either , , , or .
- The input is a rectangle, i.e., each row has the same number of columns, and there is at least one row.
- The Pacific Ocean is marked by , and the Atlantic Ocean is marked by .
- The land is marked by .
- Time complexity must be O(m * n), where is the number of rows and is the number of columns in the matrix.
- Space complexity must also be O(m * n), as extra space may be needed to store the result.
Task
Write a function that takes the input
and returns the number of cells with water flowing from the Pacific Ocean to the Atlantic Ocean.
Example
Input: {"input_data":[1,2,3]}
Output: [1,2,3]