Quick Overview

Given an integer grid and a starting square, recolor only the border squares of the connected component that contains that square and return the grid. It tests flood fill with BFS or DFS, 4-directional adjacency, and deciding which squares are border squares before changing any colors.

Recolor the border of a connected component in a grid

Company: Booking.com

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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). Color the border of the connected component that contains the square `grid[row][col]` with `color`. Return the final `grid`. ### Function Signature ```python def color_border(grid: list[list[int]], row: int, col: int, color: int) -> list[list[int]]: ``` ### Rules - `grid[r][c]` is the square in row `r` and column `c`. - Adjacency is 4-directional (up, down, left, right). Diagonal squares are not adjacent. - A connected component is every square reachable from a starting square through a chain of adjacent squares that all share its color. - Decide which squares are border squares from the colors in the input grid, then recolor them. Recoloring never changes which squares count as border. - Only the border squares of the component that contains `(row, col)` change. Every other square keeps its value. Modifying `grid` in place and returning it is fine. ### Constraints - `m == grid.length` - `n == grid[i].length` - `1 <= m, n <= 50` - `1 <= grid[i][j], color <= 1000` - `0 <= row < m` - `0 <= col < n` ### Examples ```text Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3 Output: [[3,3],[3,2]] ``` The component of `(0,0)` is the three 1s. All of them touch either the grid edge or the square with value 2, so all are border squares and become 3. ```text Input: grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3 Output: [[1,3,3],[2,3,3]] ``` The component of `(0,1)` is the 2s at `(0,1)`, `(0,2)` and `(1,2)`, and each lies on the grid edge, so all three become 3. The 2 at `(1,0)` has only a 1 and a 3 as neighbors, so it is a separate component and stays 2. ```text 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]] ``` All nine squares form one component. The center square is not on the grid edge and all four of its neighbors are in the component, so it is the only square that keeps its color.

Overview: Given an integer grid and a starting square, recolor only the border squares of the connected component that contains that square and return the grid. It tests flood fill with BFS or DFS, 4-directional adjacency, and deciding which squares are border squares before changing any colors.

You are given an m x n integer matrix grid and three integers row, col and color. Each value in grid is the color of the square at that location, and grid[r][c] is the square in row r and column c. Two squares are adjacent when they are next to each other in one of the 4 directions: up, down, left or right. Squares that touch only at a corner are not adjacent. Two squares belong to the same connected component when they have the same color and are adjacent, so a connected component is every square reachable from a starting square through a chain of adjacent squares that all share its color. The border of a connected component is all the squares in the component that are adjacent to at least one square not in the component, or that lie on the boundary of the grid (the first or last row, or the first or last column). Color the border of the connected component that contains the square grid[row][col] with color, and return the final grid. Rules: - Decide which squares are border squares from the colors in the input grid, then recolor them. Recoloring never changes which squares count as border. - Only the border squares of the component that contains (row, col) change. Every other square keeps its value. - Modifying grid in place and returning it is fine; the returned grid is what is checked. Example 1: Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3 Output: [[3,3],[3,2]] The component of (0,0) is the three 1s. Each of them touches the grid edge or the square with value 2, so all three are border squares and become 3. Example 2: 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]] All nine squares form one component. The center square is not on the grid edge and all four of its neighbors are in the component, so it is the only square that keeps its color. Constraints: - m == grid.length - n == grid[i].length - 1 <= m, n <= 50 - 1 <= grid[i][j], color <= 1000 - 0 <= row < m - 0 <= col < n Every value fits in a 32-bit signed integer, and color may equal the component's current color.

Constraints

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

Examples

Input: ([[1, 1], [1, 2]], 0, 0, 3)

Expected Output: [[3, 3], [3, 2]]

Explanation: Source example 1: the three 1s each touch the grid edge or the 2, so all of them become 3.

Input: ([[1, 2, 2], [2, 3, 2]], 0, 1, 3)

Expected Output: [[1, 3, 3], [2, 3, 3]]

Explanation: Source example 2: the 2s at (0,1), (0,2) and (1,2) form the component and all lie on the grid edge; the 2 at (1,0) is a separate component and stays 2.

Hints

  1. First work out exactly which squares belong to the component of (row, col): they share its color and can be reached through moves up, down, left or right.
  2. A component square is a border square when it lies in the first or last row or column, or when one of its four side neighbors is outside the component. Squares that touch only at a corner do not count.
  3. Settle the full set of border squares before you change any value, so an early recolor cannot make a neighbor look like a border square.

Color the Border of a Connected Region

mediumBooking.comSoftware Engineer

You are given an m x n integer matrix grid and three integers row, col and color. Each value in grid is the color of the square at that location, and grid[r][c] is the square in row r and column c.

Two squares are adjacent when they are next to each other in one of the 4 directions: up, down, left or right. Squares that touch only at a corner are not adjacent. Two squares belong to the same connected component when they have the same color and are adjacent, so a connected component is every square reachable from a starting square through a chain of adjacent squares that all share its color.

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

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

Rules:

  • Decide which squares are border squares from the colors in the input grid, then recolor them. Recoloring never changes which squares count as border.
  • Only the border squares of the component that contains (row, col) change. Every other square keeps its value.
  • Modifying grid in place and returning it is fine; the returned grid is what is checked.

Example 1: Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3 Output: [[3,3],[3,2]] The component of (0,0) is the three 1s. Each of them touches the grid edge or the square with value 2, so all three are border squares and become 3.

Example 2: 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]] All nine squares form one component. The center square is not on the grid edge and all four of its neighbors are in the component, so it is the only square that keeps its color.

Constraints:

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

Every value fits in a 32-bit signed integer, and color may equal the component's current color.

Examples

Example 1
Input
([[1, 1], [1, 2]], 0, 0, 3)
Output
[[3, 3], [3, 2]]
Notes
Source example 1: the three 1s each touch the grid edge or the 2, so all of them become 3.
Example 2
Input
([[1, 2, 2], [2, 3, 2]], 0, 1, 3)
Output
[[1, 3, 3], [2, 3, 3]]
Notes
Source example 2: the 2s at (0,1), (0,2) and (1,2) form the component and all lie on the grid edge; the 2 at (1,0) is a separate component and stays 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

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def color_border(grid: list[list[int]], row: int, col: int, color: int) -> list[list[int]]:
Sample call
color_border([[1, 1], [1, 2]], 0, 0, 3)
Language
Case 1
Input
([[1, 1], [1, 2]], 0, 0, 3)
Expected
[[3, 3], [3, 2]]
Case 2
Input
([[1, 2, 2], [2, 3, 2]], 0, 1, 3)
Expected
[[1, 3, 3], [2, 3, 3]]

Browse More Questions

Show the approach

Approach

Algorithm: walk the component of (row, col) with an explicit stack and a visited table, stepping only up, down, left or right into squares whose color equals the original color grid[row][col]. When a square is popped, examine its four neighbors: a neighbor outside the grid, or one with a different color, marks the square as a border square; a matching neighbor that has not been visited is marked and pushed. Border squares are collected in a list and recolored only after the walk ends, on a copy of the grid.

Invariant: the grid is never written during the walk, so every color comparison sees the input colors, which is exactly what the rule about deciding the border from the input grid requires.

Correctness: the walk reaches exactly the squares connected to (row, col) through same-colored side neighbors, which is the component. A same-colored side neighbor of a component square is always in the component, so a component square has a neighbor outside the component exactly when some side neighbor is off the grid or has a different color. The collected list is therefore exactly the border, and only those squares change.

Edge cases: in a 1x1 grid, a single row or a single column, every component square is on the grid edge and is recolored; when color equals the original color the returned grid equals the input; squares that touch only at a corner are never examined; the explicit stack avoids recursion depth limits on a component of up to 2,500 squares.

Time complexity:
O(m * n)
Space complexity:
O(m * n)