Water Flow Problem Statement
Given a matrix A
with dimensions 'N' x 'M', where each cell represents the height of water in that location, determine the coordinates from which water can flow to both the Pacific and Atlantic oceans. Water can only flow horizontally or vertically to neighboring cells that have a height less than or equal to the current cell.
Explanation:
The matrix boundaries act as ocean borders with the Pacific on the left and top, and the Atlantic on the right and bottom. Find all coordinates where water can flow to both oceans. Return the coordinates in lexicographical order.
Input:
The first line of input contains an integer 'T', the number of test cases.
For each test case, the first line contains two integers, 'N' and 'M', representing the number of rows and columns in the matrix.
The following 'N' lines each contain 'M' space-separated integers representing the matrix 'A'.
Output:
For each test case, the output begins with an integer 'K' representing the number of coordinates.
Each of the next 'K' lines contains the coordinates in the format: row column.
Example:
Input:
1
3 3
1 2 2
3 4 3
2 3 1
Output:
3
0 1
1 1
2 1
Constraints:
1 ≤ T ≤ 50
1 ≤ N, M ≤ 104
1 ≤ N*M ≤ 104
1 ≤ A[i][j] ≤ 109
- Time Limit: 1 second
Note:
Implement the given function. Printing is already handled.
Given a matrix representing water heights, find coordinates where water can flow to both Pacific and Atlantic oceans.
Create a function that performs a depth-first search to find coordinates where wate...read more
Top Cult.fit Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Reviews
Interviews
Salaries
Users/Month