LeetCode 1034. Coloring A Border

原题链接在这里:https://leetcode.com/problems/coloring-a-border/

题目:

Given a 2-dimensional grid of integers, each value in the grid represents the color of the grid square at that location.

Two squares belong to the same connected component if and only if they have the same color and are next to each other in any of the 4 directions.

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

Given a square at location (r0, c0) in the grid and a color, color the border of the connected component of that square with the given color, and return the final grid.

 

Example 1:

Input: grid = [[1,1],[1,2]], r0 = 0, c0 = 0, color = 3
Output: [[3, 3], [3, 2]]

Example 2:

Input: grid = [[1,2,2],[2,3,2]], r0 = 0, c0 = 1, color = 3
Output: [[1, 3, 3], [2, 3, 3]]

Example 3:

Input: grid = [[1,1,1],[1,1,1],[1,1,1]], r0 = 1, c0 = 1, color = 2
Output: [[2, 2, 2], [2, 1, 2], [2, 2, 2]]

Note:

  1. 1 <= grid.length <= 50
  2. 1 <= grid[0].length <= 50
  3. 1 <= grid[i][j] <= 1000
  4. 0 <= r0 < grid.length
  5. 0 <= c0 < grid[0].length
  6. 1 <= color <= 1000

题解:

Could use BFS to find the border.

For current cell, its 4 neighbors, if any of them is out of boundary, or different color than grid[r0][c0], this is the border. Mark it.

Here can't change to new color yet, since later neighbor's neighbor may check this value, it needs to remain to its original color when doing BFS.

After BFS, iterate the grid again and change the border color.

Time Complexity: O(m * n). m = gird.length. n = grid[0].length.

Space: O(m * n).

AC Java:

 1 class Solution {
 2     public int[][] colorBorder(int[][] grid, int r0, int c0, int color) {
 3         if(grid == null || r0 < 0 || r0 >= grid.length || c0 < 0 || c0 >= grid[0].length || grid[r0][c0] == color){
 4             return grid;
 5         }
 6         
 7         int m = grid.length;
 8         int n = grid[0].length;
 9         int oriColor = grid[r0][c0];
10         LinkedList<int []> que = new LinkedList<>();
11         boolean [][] visited = new boolean[m][n];
12         boolean [][] border = new boolean[m][n]; 
13         que.add(new int[]{r0, c0});
14         visited[r0][c0] = true;
15         int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
16         
17         while(!que.isEmpty()){
18             int [] cur = que.poll();
19             boolean isBorder = false;
20             
21             for(int [] dir : dirs){
22                 int x = cur[0] + dir[0];
23                 int y = cur[1] + dir[1];
24                 if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != oriColor){
25                     isBorder = true;
26                     continue;
27                 }
28                 
29                 if(visited[x][y]){
30                     continue;
31                 }
32                 
33                 que.add(new int[]{x, y});
34                 visited[x][y] = true;
35             }
36             
37             if(isBorder){
38                 border[cur[0]][cur[1]] = isBorder;
39             }
40         }
41         
42         for(int i = 0; i){
43             for(int j = 0; j){
44                 if(border[i][j]){
45                     grid[i][j] = color;
46                 }
47             }
48         }
49         
50         return grid;
51     }
52 }

类似Island Perimeter.

你可能感兴趣的:(LeetCode 1034. Coloring A Border)