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) a square not in the component, or on the boundary of the grid (the first or last row or column).
You should color the border of the connected component that contains the square grid[row][col]
with color
.
Return the final 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]]
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 50
1 <= grid[i][j], color <= 1000
0 <= row < m
0 <= col < n
This problem asks us to color the border of a connected component in a grid. A connected component is a group of cells with the same color that are adjacent (horizontally or vertically). The border consists of cells that are either adjacent to a cell of a different color or are on the edge of the grid.
The most efficient approach is using Depth-First Search (DFS). We perform a DFS to traverse the connected component starting from the given cell (row
, col
). During the traversal, we mark visited cells using a vis
array. Crucially, we identify border cells while exploring. A cell is a border cell if:
We mark these border cells with the new color
during the DFS.
Initialization: Create a vis
matrix (boolean array) of the same dimensions as the grid
to track visited cells. Initialize all cells to false
.
DFS Traversal: Implement a recursive DFS function:
i
, column index j
, and the original color c
of the connected component as input.(i, j)
as visited (vis[i][j] = True
).c
, recursively call DFS on it.color
.color
.Start DFS: Call the DFS function starting from the given (row, col)
.
Return: Return the modified grid
.
Time Complexity: O(M*N), where M and N are the dimensions of the grid. In the worst case, we visit every cell in the grid.
Space Complexity: O(MN) in the worst case. This is due to the vis
matrix used to track visited cells. The recursion depth of the DFS can also reach O(MN) in the worst-case scenario (a fully connected component). However, in practice, the space used by the recursive call stack is often smaller than the vis
matrix.
from itertools import pairwise
class Solution:
def colorBorder(self, grid: List[List[int]], row: int, col: int, color: int) -> List[List[int]]:
m, n = len(grid), len(grid[0])
vis = [[False] * n for _ in range(m)]
def dfs(i: int, j: int, c: int) -> None:
vis[i][j] = True
for a, b in pairwise((-1, 0, 1, 0, -1)):
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n:
if not vis[x][y]:
if grid[x][y] == c:
dfs(x, y, c)
else:
grid[i][j] = color
else:
grid[i][j] = color
dfs(row, col, grid[row][col])
return grid
The code in other languages (Java, C++, Go, TypeScript) follows the same algorithmic approach, differing only in syntax and data structure implementations. The pairwise
function in Python efficiently iterates through the four adjacent cells. Other languages may use arrays or loops to achieve the same.