Skip to main content
  1. LeetCode/

LeetCode 200: Number of Islands

·1 min· ·
LeetCode DFS Medium
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-05-10

  • Total time: 10:00.00

Intuition
#

This is a classic DFS problem.
We iterate through the grid from left to right, top to bottom.
If a cell contains '1', it indicates the start of a new island, so we increment the island count by 1.
Once we identify an island at a coordinate, we use DFS to traverse all adjacent coordinates, changing all connected land cells from '1' to '#' to mark them as visited.
This prevents us from counting the same island multiple times.

Approach
#

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        x = len(grid[0])
        y = len(grid)
        
        def dfs(i, j):
            if grid[i][j] == "#" or grid[i][j] == "0":
                return

            grid[i][j] = "#"
            
            if i>0:
                dfs(i-1, j)
            
            if i<y-1:
                dfs(i+1, j)

            if j>0:
                dfs(i, j-1)
            
            if j<x-1:
                dfs(i, j+1)

        res = 0
        for i in range(y):
            for j in range(x):
                if grid[i][j] == "1":
                    res += 1
                    dfs(i, j)

        return res

Findings
#

First I would like to create a n x m grid to record the visit path, but after I realized that I can just amend the grid.

Encountered Problems
#

Related

LeetCode 547: Number of Provinces
·3 mins
LeetCode Union Find DFS Medium
LeetCode Problem Solving
LeetCode 947: Most Stones Removed with Same Row or Column
·3 mins
LeetCode Union Find Medium
LeetCode Problem Solving
LeetCode 990: Satisfiability of Equality Equations
·1 min
LeetCode Union Find Medium
LeetCode Problem Solving
LeetCode 1202: Smallest String With Swaps
·1 min
LeetCode Union Find Medium
LeetCode Problem Solving
LeetCode 1319: Number of Operations to Make Network Connected
·2 mins
LeetCode Union Find Medium
LeetCode Problem Solving
LeetCode 684: Redundant Connection
·1 min
LeetCode Union Find Medium
LeetCode Problem Solving