#1034
Coloring A Border
specialist · 925 · lc medium +32 · verified · 51.1% accepted · 843 likes · top 40%
Description
You are given an m x n integer grid where each cell holds a color value. Two cells are adjacent if they share a side. A connected component is a maximal set of cells with equal color that are all mutually reachable.
The border of a connected component consists of cells in the component that either touch the grid boundary or are adjacent to a cell outside the component.
Recolor the border of the component containing grid[row][col] with color and return the resulting grid.
Example 1:
Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3
Output: [[3,3],[3,2]]
Example 2:
Input: grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3
Output: [[1,3,3],[2,3,3]]
Example 3:
Input: grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2
Output: [[2,2,2],[2,1,2],[2,2,2]]
Code
1
2
3