Coloring A Border

You are given an m x n integer matrix grid, and three integers row, col, and color. Each value in the grid represents the color of the grid square at that location.

Two squares are called adjacent if they are next to each other in any of the 4 directions.

Two squares belong to the same connected component if they have the same color and they are adjacent.

The border of a connected component is all the squares in the connected component that are either adjacent to at least one square not in the component, or on the boundary of the grid (the first or last row or column).

Color the border of the connected component that contains the square grid[row][col] with color.

Return the final grid.

Example 1
1 1
1 2
Inputgrid = [[1,1],[1,2]], row = 0, col = 0, color = 3
Output[[3,3],[3,2]]
The connected component containing grid[0][0] has border cells at (0,0), (0,1), and (1,0), so they are colored 3.
Example 2
1 2 2
2 3 2
Inputgrid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3
Output[[1,3,3],[2,3,3]]
The connected component containing grid[0][1] includes the cells with color 2 connected to it, and its border cells are colored 3.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 50
  • 1 <= grid[i][j], color <= 1000
  • 0 <= row < m
  • 0 <= col < n

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate