2026-06-01 18:12:40 +08:00
#+ANKI_DECK : study_deck_02
2026-06-01 17:12:10 +08:00
* TODO 1905. Count Sub Islands :medium:
2026-06-01 02:33:30 +08:00
:PROPERTIES:
2026-06-01 17:22:07 +08:00
:NEETCODE: [[file:../../roadmap.org::*1905. Count Sub Islands][1905. Count Sub Islands]]
2026-06-01 02:33:30 +08:00
:END:
2026-06-01 17:22:07 +08:00
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:*
#+begin_ src
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.
#+end_src
*Example 2:*
#+begin_ src
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.
#+end_src
*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~ .
2026-06-01 02:39:53 +08:00
** TODO Approach
Write your approach here.
** TODO Python
2026-06-05 22:32:49 +08:00
#+begin_src python :lc-problem 1905 :lc-lang python3
2026-06-01 17:22:07 +08:00
class Solution :
def countSubIslands ( self , grid1 : List [ List [ int ] ] , grid2 : List [ List [ int ] ] ) - > int :
2026-06-01 02:39:53 +08:00
#+end_src
** TODO C++
2026-06-05 22:32:49 +08:00
#+begin_src cpp :lc-problem 1905
2026-06-01 17:22:07 +08:00
class Solution {
public :
int countSubIslands ( vector < vector < int > > & grid1 , vector < vector < int > > & grid2 ) {
}
} ;
2026-06-01 02:33:30 +08:00
#+end_src