1. 程式人生 > 其它 >[LeetCode] 1905. Count Sub Islands

[LeetCode] 1905. Count Sub Islands

You are given twom x nbinary matricesgrid1andgrid2containing only0's (representing water) and1's (representing land). Anislandis a group of1's connected4-directionally(horizontal or vertical). Any cells outside of the grid are considered water cells.

An island ingrid2is considered asub-islandif there is an island ingrid1

that containsallthe cells that make upthisisland ingrid2.

Return thenumberof islands ingrid2that are consideredsub-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]andgrid2[i][j]are either0or1.

統計子島嶼。

給你兩個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的思路。

題目問的是兩個尺寸相同的矩陣 grid1 和 grid2,在 grid2 中出現的島嶼是否能被grid1中同一個島嶼完全包含。按照這個題意,我們先去 grid2 裡找一個島嶼的起點,在將當前這個島的尺寸擴張的同時,我們需要判斷在 grid2 裡是島嶼的座標,在 grid1 裡是否也是島嶼,如果不是則返回 false。

時間O(mn)

空間O(n) - 遞迴的棧空間

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 }

flood fill題型總結

LeetCode 題目總結