Implement number of islands solution using DFS#535
Open
virajkvk18 wants to merge 1 commit intocodedecks-in:masterfrom
Open
Implement number of islands solution using DFS#535virajkvk18 wants to merge 1 commit intocodedecks-in:masterfrom
virajkvk18 wants to merge 1 commit intocodedecks-in:masterfrom
Conversation
virajkvk18
commented
Apr 6, 2026
Author
virajkvk18
left a comment
There was a problem hiding this comment.
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! 🙌**
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 separatevismatrix. 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?
vismatrix and DFS recursion stackvismatrix instead of modifying the input grid to preserve original data.How Has This Been Tested?
The solution was tested against the following cases:
'0's → returns0'1's → returns1(one large island)'1'→ returns1All test cases were verified against LeetCode's online judge (Accepted).
Make sure all below guidelines are followed else PR will get Rejected: