Sale
Summer Offer: $190/yr $99/yr
00 : 00 : 00 : 00
Get this deal
Backend Engineering Senior programming

LeetCode #1034 - Color A Border


You are given a 2D grid of integers, where each integer represents the color of the cell. You need to color the border of a connected component of the grid with a new color. A border is defined as a cell that is part of the component but is adjacent to at least one cell that is not part of the component. Please implement a function that achieves this.

Input/Output


Input:
  • grid: List[List[int]] (the grid of colors)

  • row: int (starting row)

  • col: int (starting column)

  • color: int (new color)

Output:
  • List[List[int]] (the modified grid)


Constraints


1 <= grid.length, grid[0].length <= 50
0 <= grid[i][j] <= 100
0 <= row < grid.length
0 <= col < grid[0].length
0 <= color <= 100

Example


Input:
[[1,1,1],[1,1,0],[1,0,1]]
1
1
2
Output:
[[2,2,2],[2,2,0],[2,0,1]]
Suggested Answer

Trusted by 100+ professionals preparing for interviews

Trusted by 100+ professionals 50+ Company Question Banks 5+ Supported Languages

Practice More Questions Like This

Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.

Get Started Free

More Backend Engineering Interview Prep

Describe a scenario in which you were required to refactor an existing service. What were the challenges you faced and how did you approach them?
Backend Engineering · Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Link copied to clipboard