|
| 1 | +<h2><a href="https://leetcode.com/problems/coloring-a-border">Coloring A Border</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>You are given an <code>m x n</code> integer matrix <code>grid</code>, and three integers <code>row</code>, <code>col</code>, and <code>color</code>. Each value in the grid represents the color of the grid square at that location.</p> |
| 2 | + |
| 3 | +<p>Two squares are called <strong>adjacent</strong> if they are next to each other in any of the 4 directions.</p> |
| 4 | + |
| 5 | +<p>Two squares belong to the same <strong>connected component</strong> if they have the same color and they are adjacent.</p> |
| 6 | + |
| 7 | +<p>The <strong>border of a connected component</strong> 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).</p> |
| 8 | + |
| 9 | +<p>You should color the <strong>border</strong> of the <strong>connected component</strong> that contains the square <code>grid[row][col]</code> with <code>color</code>.</p> |
| 10 | + |
| 11 | +<p>Return <em>the final grid</em>.</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | +<p><strong class="example">Example 1:</strong></p> |
| 15 | +<pre><strong>Input:</strong> grid = [[1,1],[1,2]], row = 0, col = 0, color = 3 |
| 16 | +<strong>Output:</strong> [[3,3],[3,2]] |
| 17 | +</pre><p><strong class="example">Example 2:</strong></p> |
| 18 | +<pre><strong>Input:</strong> grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3 |
| 19 | +<strong>Output:</strong> [[1,3,3],[2,3,3]] |
| 20 | +</pre><p><strong class="example">Example 3:</strong></p> |
| 21 | +<pre><strong>Input:</strong> grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2 |
| 22 | +<strong>Output:</strong> [[2,2,2],[2,1,2],[2,2,2]] |
| 23 | +</pre> |
| 24 | +<p> </p> |
| 25 | +<p><strong>Constraints:</strong></p> |
| 26 | + |
| 27 | +<ul> |
| 28 | + <li><code>m == grid.length</code></li> |
| 29 | + <li><code>n == grid[i].length</code></li> |
| 30 | + <li><code>1 <= m, n <= 50</code></li> |
| 31 | + <li><code>1 <= grid[i][j], color <= 1000</code></li> |
| 32 | + <li><code>0 <= row < m</code></li> |
| 33 | + <li><code>0 <= col < n</code></li> |
| 34 | +</ul> |
0 commit comments