-
Notifications
You must be signed in to change notification settings - Fork 14
/
733-flood-fill.java
33 lines (32 loc) · 1.15 KB
/
733-flood-fill.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
int oldColor = image[sr][sc];
if (oldColor == color) {
return image;
}
int rows = image.length;
int cols = image[0].length;
ArrayDeque<int[]> queue = new ArrayDeque<>();
HashSet<Integer> visited = new HashSet<>();
queue.offer(new int[]{sr, sc});
visited.add(sr * cols + sc);
while (!queue.isEmpty()) {
int[] rc = queue.poll();
int r = rc[0];
int c = rc[1];
image[r][c] = color;
for (int[] d: new int[][]{{1, 0}, {- 1, 0}, {0, 1}, {0, - 1}}) {
int nextR = r + d[0];
int nextC = c + d[1];
if (0 <= nextR && nextR < rows && 0 <= nextC && nextC < cols && !visited.contains(nextR * cols + nextC) && image[nextR][nextC] == oldColor) {
queue.offer(new int[]{nextR, nextC});
visited.add(nextR * cols + nextC);
}
}
}
return image;
}
}
// time O(RC)
// space O(RC)
// using graph and bfs with single source