Post

LeetCode 200. Number of Islands

LeetCode 200. Number of Islands

LeetCode 200. Number of Islands

LINK: https://leetcode.com/problems/number-of-islands/

Matter

You are given a 2D grid consisting of ‘1’ (land) and ‘0’ (water).

Count how many separate islands exist.
An island is formed by connecting adjacent land cells horizontally or vertically.

Example.

[1]
Input:
grid = [ [“1”,”1”,”1”,”1”,”0”], [“1”,”1”,”0”,”1”,”0”], [“1”,”1”,”0”,”0”,”0”], [“0”,”0”,”0”,”0”,”0”] ]

Output:
1

[2]
Input:
grid = [ [“1”,”1”,”0”,”0”,”0”], [“1”,”1”,”0”,”0”,”0”], [“0”,”0”,”1”,”0”,”0”], [“0”,”0”,”0”,”1”,”1”] ]

Output:
3

Constraints

1 <= m, n <= 300

grid[i][j] is ‘0’ or ‘1’

Problem analysis

This is a classic connected-components problem on a grid.

Each island is a group of connected ‘1’ cells.

The idea is:

  • Traverse the grid
  • When a ‘1’ is found → start a DFS/BFS
  • Mark all connected land as visited
  • Increase island count

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Solution {
public:
    int numIslands(std::vector<std::vector<char>>& grid) 
    {
        int i32Count = 0;

        do
        {
            if(!grid.size())
                break;

            int i32RowSize = grid.size();

            for(int i32Row = 0; i32Row < i32RowSize; ++i32Row)
            {
                std::vector<char>& vctRow = grid[i32Row];
                int i32ColSize = vctRow.size();

                for(int i32Col = 0; i32Col < i32ColSize; ++i32Col)
                {
                    if(vctRow[i32Col] == '1')
                    {
                        CheckAdjacent(grid, i32Row, i32Col);    
                        ++i32Count;
                    }
                }
            }
        }
        while (false);

        return i32Count;
    }
private:
    bool CheckAdjacent(std::vector<std::vector<char>>& grid, int i32Row, int i32Col)
    {
        bool bReturn = false;

        do
        {
            int i32RowSize = grid.size();
            int i32ColSize = grid[i32Row].size();

            auto IsValid = [&i32RowSize, &i32ColSize](int i32Row, int i32Col) -> bool
            {
                return i32Row >= 0 && i32Row < i32RowSize && 
                        i32Col >= 0 && i32Col < i32ColSize;
            };

            std::vector<std::pair<int, int>> vctland;
            vctland.push_back({i32Row, i32Col});

            while(!vctland.empty())
            {
                std::pair<int, int> land = vctland.back();
                vctland.pop_back();
                int i32Row = land.first;
                int i32Col = land.second;

                char* pI8Cur = &grid[i32Row][i32Col];

                if(IsValid(i32Row, i32Col) && *pI8Cur == '1')
                    {
                        *pI8Cur = '0';

                        if(IsValid(i32Row, i32Col + 1) && *(pI8Cur + 1) == '1')
                            vctland.push_back({i32Row, i32Col + 1});

                        if(IsValid(i32Row, i32Col - 1) && *(pI8Cur - 1) == '1')
                            vctland.push_back({i32Row, i32Col - 1});

                        if(IsValid(i32Row - 1, i32Col) && grid[i32Row - 1][i32Col] == '1')
                            vctland.push_back({i32Row - 1, i32Col});

                        if(IsValid(i32Row + 1, i32Col) && grid[i32Row + 1][i32Col] == '1')
                            vctland.push_back({i32Row + 1, i32Col});
                    }
            }

            bReturn = true;
        } 
        while (false);
        
        return bReturn;
    }
};
Little bit more faster:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Solution {
public:
    int numIslands(std::vector<std::vector<char>>& grid) 
    {
        int i32Count = 0;

        do
        {
            if(!grid.size())
                break;

            int i32RowSize = grid.size();

            for(int i32Row = 0; i32Row < i32RowSize; ++i32Row)
            {
                std::vector<char>& vctRow = grid[i32Row];
                int i32ColSize = vctRow.size();

                for(int i32Col = 0; i32Col < i32ColSize; ++i32Col)
                {
                    if(vctRow[i32Col] == '1')
                    {
                        CheckAdjacent(grid, i32Row, i32Col);    
                        ++i32Count;
                    }
                }
            }
        }
        while (false);

        return i32Count;
    }
private:
    bool CheckAdjacent(std::vector<std::vector<char>>& grid, int i32Row, int i32Col)
    {
        bool bReturn = false;

        do
        {
            int i32RowSize = grid.size();
            int i32ColSize = grid[i32Row].size();

            std::vector<std::pair<int, int>> vctland;
            vctland.push_back({i32Row, i32Col});

            while(!vctland.empty())
            {
                std::pair<int, int> land = vctland.back();
                vctland.pop_back();
                int i32Row = land.first;
                int i32Col = land.second;

                char* pI8Cur = &grid[i32Row][i32Col];

                if(i32Row >= 0 && i32Row < i32RowSize && 
                        i32Col >= 0 && i32Col < i32ColSize && 
                        *pI8Cur == '1')
                    {
                        *pI8Cur = '0';

                        if(i32Col + 1 < i32ColSize && *(pI8Cur + 1) == '1')
                            vctland.push_back({i32Row, i32Col + 1});

                        if(i32Col > 0 && *(pI8Cur - 1) == '1')
                            vctland.push_back({i32Row, i32Col - 1});

                        if(i32Row > 0 && grid[i32Row - 1][i32Col] == '1')
                            vctland.push_back({i32Row - 1, i32Col});

                        if(i32Row + 1 < i32RowSize && grid[i32Row + 1][i32Col] == '1')
                            vctland.push_back({i32Row + 1, i32Col});
                    }
            }

            bReturn = true;
        } 
        while (false);
        
        return bReturn;
    }
};
Possible, But somtimes stack-overflow issue:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Solution {
public:
    int numIslands(std::vector<std::vector<char>>& grid) 
    {
        int i32Count = 0;

        do
        {
            if(!grid.size())
                break;

            int i32RowSize = grid.size();

            for(int i32Row = 0; i32Row < i32RowSize; ++i32Row)
            {
                std::vector<char>& vctRow = grid[i32Row];
                int i32ColSize = vctRow.size();

                for(int i32Col = 0; i32Col < i32ColSize; ++i32Col)
                {
                    if(vctRow[i32Col] == '1')
                    {
                        CheckAdjacent(grid, i32Row, i32Col);    
                        ++i32Count;
                    }
                }
            }
        }
        while (false);

        return i32Count;
    }
private:
    bool CheckAdjacent(std::vector<std::vector<char>>& grid, int i32Row, int i32Col)
    {
        bool bReturn = false;

        do
        {
            if(i32Row < 0 || 
                i32Row >= grid.size() || 
                i32Col < 0 || 
                i32Col >= grid[i32Row].size())
                break;

            if(grid[i32Row][i32Col] != '1')
                break;

            grid[i32Row][i32Col] = '0';

            CheckAdjacent(grid, i32Row - 1, i32Col);
            CheckAdjacent(grid, i32Row + 1, i32Col);
            CheckAdjacent(grid, i32Row, i32Col - 1);
            CheckAdjacent(grid, i32Row, i32Col + 1);

            bReturn = true;
        } 
        while (false);
        
        return bReturn;
    }
};
This post is licensed under CC BY 4.0 by the author.