1905. Count Sub Islands Medium
1/**
2 * [1905] Count Sub Islands
3 *
4 * 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.
5 * 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.
6 * Return the number of islands in grid2 that are considered sub-islands.
7 *
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/test1.png" style="width: 493px; height: 205px;" />
10 * 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]]
11 * Output: 3
12 * Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
13 * The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
14 *
15 * Example 2:
16 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/03/testcasex2.png" style="width: 491px; height: 201px;" />
17 * 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]]
18 * Output: 2
19 * Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
20 * The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
21 *
22 *
23 * Constraints:
24 *
25 * m == grid1.length == grid2.length
26 * n == grid1[i].length == grid2[i].length
27 * 1 <= m, n <= 500
28 * grid1[i][j] and grid2[i][j] are either 0 or 1.
29 *
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/count-sub-islands/
34// discuss: https://leetcode.com/problems/count-sub-islands/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39 pub fn count_sub_islands(grid1: Vec<Vec<i32>>, grid2: Vec<Vec<i32>>) -> i32 {
40 0
41 }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_1905() {
52 }
53}
54
Back
© 2025 bowen.ge All Rights Reserved.