-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathProblem_3_Flood_Fill.java
More file actions
23 lines (20 loc) · 843 Bytes
/
Problem_3_Flood_Fill.java
File metadata and controls
23 lines (20 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package Island_Matrix_Traversal;
// Problem Statement: Flood Fill (easy)
// LeetCode Question: 733. Flood Fill
public class Problem_3_Flood_Fill {
public int[][] floodFill(int[][] matrix, int x, int y, int newColor){
if (matrix[x][y] != newColor) {
fillDFS(matrix, x, y, matrix[x][y], newColor);
}
return matrix;
}
private static void fillDFS(int[][] matrix, int x, int y, int oldColor, int newColor){
if (x < 0 || x >= matrix.length || y < 0 || y >= matrix[0].length) return;
if (matrix[x][y] != oldColor) return;
matrix[x][y] = newColor;
fillDFS(matrix, x + 1, y, oldColor, newColor);
fillDFS(matrix, x - 1, y, oldColor, newColor);
fillDFS(matrix, x, y + 1, oldColor, newColor);
fillDFS(matrix, x, y - 1, oldColor, newColor);
}
}