-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
49 lines (42 loc) · 1.18 KB
/
main.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// URL: https://leetcode.com/problems/flood-fill/
/**
* @param {number[][]} image
* @param {number} sr
* @param {number} sc
* @param {number} color
* @return {number[][]}
*/
const floodFill = (image, sr, sc, newColor) => {
const oldColor = image[sr][sc];
if(oldColor !== newColor) rePaint(image, sr, sc, oldColor, newColor);
return image;
};
const rePaint = (image, sr, sc, oldColor, newColor) => {
// Make sure we are inside the grid
const NotInsideTheGridAnyMore = sr < 0 || sr >= image.length || sc < 0 || sc >= image[sr].length;
if(NotInsideTheGridAnyMore) {
return;
}
// Encountered a color boundry
if(image[sr][sc] !== oldColor) {
return;
}
// Update color
image[sr][sc] = newColor;
// Check all 4 sides of the given pixel
rePaint(image, sr + 1, sc, oldColor, newColor);
rePaint(image, sr - 1, sc, oldColor, newColor);
rePaint(image, sr, sc + 1, oldColor, newColor);
rePaint(image, sr, sc - 1, oldColor, newColor);
}
let img1 = [
[1, 1, 1],
[1, 1, 0],
[1, 0, 1],
];
console.log(floodFill(img1, 1, 1, 2));
let img2 = [
[0, 0, 0],
[0, 0, 0],
];
console.log(floodFill(img2, 0, 0, 0));