zoukankan      html  css  js  c++  java
  • [LeetCode] 1905. Count Sub Islands

    You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land). An island is a group of 1's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.

    An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.

    Return the number of islands in grid2 that are considered sub-islands.

    Example 1:

    Input: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
    Output: 3
    Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
    The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
    

    Example 2:

    Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
    Output: 2 
    Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
    The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.

    Constraints:

    • m == grid1.length == grid2.length
    • n == grid1[i].length == grid2[i].length
    • 1 <= m, n <= 500
    • grid1[i][j] and grid2[i][j] are either 0 or 1.

    统计子岛屿。

    给你两个 m x n 的二进制矩阵 grid1 和 grid2 ,它们只包含 0 (表示水域)和 1 (表示陆地)。一个 岛屿 是由 四个方向 (水平或者竖直)上相邻的 1 组成的区域。任何矩阵以外的区域都视为水域。

    如果 grid2 的一个岛屿,被 grid1 的一个岛屿 完全 包含,也就是说 grid2 中该岛屿的每一个格子都被 grid1 中同一个岛屿完全包含,那么我们称 grid2 中的这个岛屿为 子岛屿 。

    请你返回 grid2 中 子岛屿 的 数目 。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/count-sub-islands
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道典型的flood fill类型的题,做法也类似于200题。我给出 DFS 和 BFS 的思路。

    题目问的是两个尺寸相同的矩阵 grid1 和 grid2,在 grid2 中出现的岛屿是否能被 grid1 中同一个岛屿完全包含。按照这个题意,我们先去 grid2 里找一个岛屿的起点,在将当前这个岛的尺寸扩张的同时,我们需要判断在 grid2 里是岛屿的坐标,在 grid1 里是否也是岛屿,如果不是则返回 false。只有 grid2 的当前岛屿可以完全被 grid1 覆盖,才是子岛屿。

    DFS

    时间O(mn)

    空间O(mn) - 递归的栈空间

    Java实现

     1 class Solution {
     2     int[][] dirs = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
     3     int rows;
     4     int cols;
     5     int[][] grid1;
     6     int[][] grid2;
     7 
     8     public int countSubIslands(int[][] grid1, int[][] grid2) {
     9         rows = grid2.length;
    10         cols = grid2[0].length;
    11         this.grid1 = grid1;
    12         this.grid2 = grid2;
    13         int count = 0;
    14         for (int i = 0; i < rows; i++) {
    15             for (int j = 0; j < cols; j++) {
    16                 if (grid2[i][j] == 1 && dfs(i, j)) {
    17                     count++;
    18                 }
    19             }
    20         }
    21         return count;
    22     }
    23 
    24     private boolean dfs(int i, int j) {
    25         boolean flag = true;
    26         grid2[i][j] = 0;
    27         if (grid1[i][j] != 1) {
    28             flag = false;
    29         }
    30         for (int[] d : dirs) {
    31             int newX = i + d[0];
    32             int newY = j + d[1];
    33             if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && grid2[newX][newY] == 1) {
    34                 if (!dfs(newX, newY)) {
    35                     flag = false;
    36                 }
    37             }
    38         }
    39         return flag;
    40     }
    41 }

    BFS

    时间O(mn)

    空间O(mn) - 递归的栈空间

    Java实现

     1 class Solution {
     2     int[][] dirs = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
     3     int rows;
     4     int cols;
     5     int[][] grid1;
     6     int[][] grid2;
     7 
     8     public int countSubIslands(int[][] grid1, int[][] grid2) {
     9         rows = grid1.length;
    10         cols = grid1[0].length;
    11         this.grid1 = grid1;
    12         this.grid2 = grid2;
    13         int count = 0;
    14         for (int i = 0; i < rows; i++) {
    15             for (int j = 0; j < cols; j++) {
    16                 if (grid2[i][j] == 1 && bfs(i, j)) {
    17                     count++;
    18                 }
    19             }
    20         }
    21         return count;
    22     }
    23 
    24     private boolean bfs(int i, int j) {
    25         Queue<int[]> queue = new LinkedList<>();
    26         queue.offer(new int[] { i, j });
    27         grid2[i][j] = 0;
    28         boolean flag = true;
    29         if (grid1[i][j] != 1) {
    30             flag = false;
    31         }
    32         while (!queue.isEmpty()) {
    33             int[] cur = queue.poll();
    34             for (int[] d : dirs) {
    35                 int newX = cur[0] + d[0];
    36                 int newY = cur[1] + d[1];
    37                 if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && grid2[newX][newY] == 1) {
    38                     queue.offer(new int[] { newX, newY });
    39                     grid2[newX][newY] = 0;
    40                     if (grid1[newX][newY] == 0) {
    41                         flag = false;
    42                     }
    43                 }
    44             }
    45         }
    46         return flag;
    47     }
    48 }

    flood fill题型总结

    LeetCode 题目总结

  • 相关阅读:
    R必学包之dplyr
    PSO(Thepopularity-similarity-oplimization) modol
    社区发现(Community Detection)算法
    Louvain algorithm for community detection
    路由算法
    github学习
    win10+Linux18.04双系统安装
    KBEngine源码:Entity
    skynet源码分析:timer
    skynet源码分析:Socket
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14972472.html
Copyright © 2011-2022 走看看