Skip to content

200. Number of Islands #109

Answered by topugit
mah-shamim asked this question in Q&A
Jul 27, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

We can use Depth-First Search (DFS) to explore and count the islands in the given 2D binary grid. The approach involves traversing the grid and performing DFS to mark all connected land cells for each island.

Let's implement this solution in PHP: 200. Number of Islands

<?php
class Solution {

    /**
     * @param String[][] $grid
     * @return Integer
     */
    function numIslands($grid) {
        if (empty($grid) || empty($grid[0])) {
            return 0;
        }

        $m = count($grid);
        $n = count($grid[0]);
        $islands = 0;

        for ($i = 0; $i < $m; $i++) {
            for ($j = 0; $j < $n; $j++) {
                if ($grid[$i][$j] == '1') {
                …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mah-shamim
Comment options

mah-shamim Aug 20, 2024
Maintainer Author

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants