Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
O(m*n)
, m being the rows of the matrix and n being the columns of matrix. Space complexity should be O(m*n)
.HAPPY CASE
Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -> Pacific Ocean
[0,4] -> Atlantic Ocean
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
[1,3] -> [1,4] -> Atlantic Ocean
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
[1,4] -> Atlantic Ocean
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
[3,0]: [3,0] -> Pacific Ocean
[3,0] -> [4,0] -> Atlantic Ocean
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
[3,1] -> [4,1] -> Atlantic Ocean
[4,0]: [4,0] -> Pacific Ocean
[4,0] -> Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans..
Input: grid = [[0,2,2,0]]
Output: [[0,0],[0,1],[0,2][0,3]]
EDGE CASE
Input: heights = [[1]]
Output: [[0,0]]
Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For 2D-Array, common solution patterns include:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Lets gather all spaces that touches the pacific ocean and all spaces that touches the atlantic ocean, the union of both would result in spaces that touches both the pacific ocean and atlantic ocean.
main function:
0) Write the helper dfs function
1) Initialize a variable to keep track of the coordinates that touch the pacific or atlantic
2) Iterate over the top row and bottom row
3) Iterate over the left most column and right most column
4) Return the union of touch pacific and atlantic
helper dfs function:
0) Basecase: Out of bound or space has been added to touchesOcean, return
1) If the coordinate has a height that is equal or greater than previous maxHeight, then it touches ocean
a) Add this coordinate to the touchesOcean set
b) Check the other for neighbors from this coordinate
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
def dfs(i, j, touchesOcean, maxHeight):
# Basecase: Out of bound or space has been added to touchesOcean, return
if i < 0 or i >= len(heights) or j < 0 or j >= len(heights[0]) or (i,j) in touchesOcean:
return
# If the coordinate has a height that is equal or greater than previous maxHeight, then it touches ocean
elif heights[i][j] >= maxHeight:
# Add this coordinate to the touchesOcean set
touchesOcean.add((i,j))
# Check the other for neighbors from this coordinate
dfs(i-1,j,touchesOcean,heights[i][j])
dfs(i+1,j,touchesOcean,heights[i][j])
dfs(i,j-1,touchesOcean,heights[i][j])
dfs(i,j+1,touchesOcean,heights[i][j])
# Initialize a variable to keep track of the coordinates that touch the pacific or atlantic
touchesPacific, touchesAtlantic = set(), set()
# Iterate over the top row and bottom row
for j in range(len(heights[0])):
dfs(0,j, touchesPacific, 0)
dfs(len(heights)-1,j, touchesAtlantic, 0)
# Iterate over the left most column and right most column
for i in range(len(heights)):
dfs(i,0, touchesPacific, 0)
dfs(i,len(heights[0])-1, touchesAtlantic, 0)
# Return the union of touch pacific and atlantic
return list(touchesPacific & touchesAtlantic)
class Solution {
public List<List<Integer>> pacificAtlantic(int[][] heights) {
// Initialize a variable to keep track of the coordinates that touch the pacific or atlantic
int rows = heights.length, cols = heights[0].length;
boolean[][] pac = new boolean[rows][cols];
boolean[][] atl = new boolean[rows][cols];
// Iterate over the top row and bottom row
for (int col = 0; col< cols; col++){
dfs(0, col, rows, cols, pac, heights[0][col], heights);
dfs(rows-1, col,rows, cols, atl, heights[rows-1][col], heights);
}
// Iterate over the left most column and right most column
for (int row = 0; row<rows; row++){
dfs(row, 0,rows, cols, pac, heights[row][0], heights);
dfs(row, cols-1,rows, cols, atl, heights[row][cols-1], heights);
}
// Return the union of touch pacific and atlantic
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++){
if (pac[i][j] && atl[i][j])
result.add(Arrays.asList(i,j));
}
return result;
}
private void dfs(int row, int col, int rows, int cols, boolean[][] visited, int prevHeight, int[][] heights){
// Basecase: Out of bound or space has been added to touchesOcean, return
// If the coordinate has a height that is equal or greater than previous maxHeight, then it touches ocean
if (row < 0 || row >= rows || col < 0 || col >= cols || visited[row][col] || prevHeight > heights[row][col])
return;
// Add this coordinate to the touchesOcean set
visited[row][col]= true;
// Check the other for neighbors from this coordinate
dfs(row+1, col, rows, cols, visited, heights[row][col], heights);
dfs(row-1, col, rows, cols, visited, heights[row][col], heights);
dfs(row, col+1, rows, cols, visited, heights[row][col], heights);
dfs(row, col-1, rows, cols, visited, heights[row][col], heights);
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of rows in 2D-array.
AssumeM
represents the number of columns in 2D-array.
O(N * M)
we may need to view each item in the 2D-ArrayO(N * M)
, the cost of storing the touches Pacific and touches Atlantic sets may contain all the items in the 2D-Array.