1007. Coloring Border

EasyGraphsGridDFSBFS

Given a grid of integer colors and a starting cell (row, col), consider the connected component (4-directionally, cells with the same color as the start). Recolor only the border cells of that component — cells adjacent to a different color or to the grid edge — with the given color. Return the updated grid. The input is JSON {grid, row, col, color}.

Input: JSON {grid, row, col, color}.

Output: Nested array — the updated grid.

Examples

Example 1
Input: {"grid":[[1,1],[1,2]],"row":0,"col":0,"color":3}
Output: [[3,3],[3,2]]
Explanation: All three 1s are border cells.
Example 2
Input: {"grid":[[1,2,2],[2,3,2]],"row":0,"col":1,"color":2}
Output: [[1,2,2],[2,3,2]]
Explanation: Single-cell component already border-colored.
Example 3
Input: {"grid":[[1]],"row":0,"col":0,"color":5}
Output: [[5]]
Explanation: Single cell is its own border.

Constraints

Asked by

MicrosoftGoogle
Solve this problem in the editor →