-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathFlood Fill.js
42 lines (34 loc) · 1.13 KB
/
Flood Fill.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
// Runtime: 149 ms (Top 9.09%) | Memory: 44.8 MB (Top 18.89%)
/**
* @param {number[][]} image
* @param {number} sr
* @param {number} sc
* @param {number} color
* @return {number[][]}
*/
var floodFill = function(image, sr, sc, color) {
const pixelsToCheck = [[sr, sc]]
const startingPixelColor = image[sr][sc]
const directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
const seenPixels = new Set()
if (
startingPixelColor === undefined ||
startingPixelColor === color
) return image
for (const pixel of pixelsToCheck) {
const currentPixelColor = image[pixel[0]]?.[pixel[1]]
if (
currentPixelColor === undefined ||
startingPixelColor !== currentPixelColor
) continue
image[pixel[0]][pixel[1]] = color
for (const direction of directions) {
const newPixel = [pixel[0] + direction[0], pixel[1] + direction[1]]
const pixelString = newPixel.join()
if (seenPixels.has(pixelString)) continue
pixelsToCheck.push(newPixel)
seenPixels.add(pixelString)
}
}
return image
};