Flood Fill
An image is stored as an m x n grid of integers, image, where each integer is the colour of one pixel. You are also given a starting pixel (sr, sc) and a new colour color.
Perform a flood fill from the starting pixel, like the paint bucket tool in a drawing program: the starting pixel, and every pixel joined to it by a chain of pixels with the same original colour, gets the new colour. Pixels are joined only when they are next to each other horizontally or vertically, never diagonally.
Return the image after the fill. You may change image in place and return it.
Constraints
1 <= m, n <= 500 <= image[r][c], color < 2^160 <= sr < mand0 <= sc < n
Examples
Example 1
Input: image = [[3,3,0],[3,0,1],[3,3,3]], sr = 0, sc = 0, color = 7 Output: [[7,7,0],[7,0,1],[7,7,7]]
The start pixel has colour 3. Every 3 joined to it through up/down/left/right steps becomes 7. The 0s and the 1 are untouched.
Example 2
Input: image = [[2,2],[2,2]], sr = 0, sc = 1, color = 2 Output: [[2,2],[2,2]]
The new colour equals the old one, so the image doesn't change. (A fill that doesn't check this keeps revisiting the same pixels.)
Example 3
Input: image = [[5,0,5],[0,5,0],[5,0,5]], sr = 1, sc = 1, color = 8 Output: [[5,0,5],[0,8,0],[5,0,5]]
The centre's neighbours are all 0. The corner 5s only touch it diagonally, which doesn't count, so only the centre changes.
JavaScript you'll use: Graphs and grids in JavaScript Recursion, the call stack and memoization
/**
* @param {number[][]} image
* @param {number} sr
* @param {number} sc
* @param {number} color
* @return {number[][]}
*/
function floodFill(image, sr, sc, color) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.