Skip to content

Implement number of islands solution using DFS#535

Open
virajkvk18 wants to merge 1 commit intocodedecks-in:masterfrom
virajkvk18:master
Open

Implement number of islands solution using DFS#535
virajkvk18 wants to merge 1 commit intocodedecks-in:masterfrom
virajkvk18:master

Conversation

@virajkvk18
Copy link
Copy Markdown

Pull Request Template


Description

Problem: LeetCode #200 - Number of Islands

Given a 2D binary grid of '1's (land) and '0's (water), the goal is to count the number of distinct islands.

Approach: Depth-First Search (DFS)

For each unvisited land cell ('1'), a DFS is triggered that marks all connected land cells (up, down, left, right) as visited using a separate vis matrix. Each DFS call from an unvisited land cell corresponds to discovering one new island, so the island counter is incremented after each DFS. This ensures every connected component of land is counted exactly once.

Dependencies: None. Standard C++ STL (vector) is used.


Put check marks:

Have you made changes in the [README](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/README.md) file?

  • Added problem & solution under correct topic. (Graph / BFS-DFS)
  • Specified Space & Time complexity.
    • Time Complexity: O(M × N) — each cell is visited at most once
    • Space Complexity: O(M × N) — for the vis matrix and DFS recursion stack
  • Specified difficulty level, tag & Note(if any).
    • Difficulty: Medium
    • Tag: Graph, DFS, Matrix
    • Note: Uses an explicit vis matrix instead of modifying the input grid to preserve original data.

How Has This Been Tested?

The solution was tested against the following cases:

  • Test A — Standard case: Grid with multiple disconnected islands → returns correct island count
  • Test B — Edge case: Grid filled entirely with '0's → returns 0
  • Test C — Edge case: Grid filled entirely with '1's → returns 1 (one large island)
  • Test D — Single cell grid with '1' → returns 1
  • Test E — Single row / single column grids → returns correct count

All test cases were verified against LeetCode's online judge (Accepted).


Make sure all below guidelines are followed else PR will get Rejected:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code so that it is easy to understand
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

@virajkvk18 virajkvk18 closed this Apr 6, 2026
Copy link
Copy Markdown
Author

@virajkvk18 virajkvk18 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey everyone! 👋

I've added my solution for LeetCode #200 - Number of Islands to the repository.

What I did:

  • Added a new C++ solution file for Problem #200
  • Used a DFS (Depth-First Search) approach with a separate visited matrix to avoid mutating the input grid
  • Traversal is done in all 4 directions (up, down, left, right) using a direction vector

Complexity:

  • 🕐 Time: O(M × N)
  • 💾 Space: O(M × N)

The solution has been tested on LeetCode and is accepted. Happy to make any changes based on feedback. Looking forward to your review! 🙌**

@virajkvk18 virajkvk18 reopened this Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant