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?
What do we do with 0s?
How can we traverse this graph?
What if the image is null?
HAPPY CASE
Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
Output: [[0,0,0],[0,0,0]]
EDGE CASE
Input: [[1,0,0],[0,1,0],[0,0,1]], sr = 0, sc = 0, color = 0
Output: [[0,0,0],[0,1,0],[0,0,1]]
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: Use DFS to traverse the matrix and then recursively call function on neighbors above, below, left, and right.
image
, sr
, and sc
. sr
represents the row, and sc
represents the column. ⚠️ Common Mistakes
image
.Implement the code to solve the algorithm.
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
// Check if our starting point color needs to be changed:
if (image[sr][sc] == color) return image;
// Otherwise continue. Starts from the starting pixel, changes itself to the new color which is ‘2’ in this case. And run a dfs call to check and change all it's neighbors.
dfs(image, sr, sc, image[sr][sc], color);
// return doctored image
return image;
}
// Using a dfs call we. can checks its neighbors (left, right, top, bottom), replaces those that had the same number as the one the starting pixel had (which is 1) before it changed to the new color (2). So it changes all the 1s to 2s as long as they are neighbors. Then moves to its left neighbor (1st column) to go through the same process.
private void dfs(int[][] image, int sr, int sc, int originalColor, int newColor) {
// Check if neighbor is inbound and is the same as the orginalColor.
// if it is out of bound or a different color, then stop
if (sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] != originalColor) return;
// else repeat.
image[sr][sc] = newColor;
dfs(image, sr + 1, sc, originalColor, newColor);
dfs(image, sr - 1, sc, originalColor, newColor);
dfs(image, sr, sc + 1, originalColor, newColor);
dfs(image, sr, sc - 1, originalColor, newColor);
}
}
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
# Check if our starting point color needs to be changed: If color is as intended then return original image. Otherwise continue.
if (image[sr][sc] == color):
return image
# Store our starting point color in a variable. We are given our starting point through the parameters `image`, `sr`, and `sc`. `sr` represents the row, and `sc` represents the column.
m, n, originalColor = len(image), len(image[0]), image[sr][sc]
# Using a dfs call we. can checks its neighbors (left, right, top, bottom), replaces those that had the same number as the one the starting pixel had (which is 1) before it changed to the new color (2). So it changes all the 1s to 2s as long as they are neighbors. Then moves to its left neighbor (1st column) to go through the same process.
def dfs(i,j):
# Check if neighbor is inbound and is the same as the orginalColor.
if i < 0 or i > m-1 or j < 0 or j > n-1 or image[i][j] != originalColor:
# if it is out of bound or a different color, then stop
return
# else repeat.
image[i][j] = color
dfs(i+1, j)
dfs(i-1, j)
dfs(i, j+1)
dfs(i, j-1)
# Starts from the middle as the starting pixel, changes itself to the new color which is ‘2’ in this case. And run a dfs call to check and change all it's neighbors.
dfs(sr, sc)
# Return the doctored image
return image
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.
O(N)
, where N is the number of pixels in the image. We might process every pixel.O(N)
, the size of the implicit call stack when calling DFS.