[leetcode] Number of Islands 3


Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110
11010
11000
00000

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Depth first search or Breadth first search would both works.

DFS approach.

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int count = 0;
        for(size_t i = 0; i < grid.size(); i++){
            for(size_t j = 0; j < grid[0].size(); j++){
                if(grid[i][j] == '1'){
                    DFS(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }
    void DFS(vector<vector<char>>& grid, int row, int col){
        int dict[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
        //boundary check
        grid[row][col] = '0';
        for(int i = 0; i < 4; i++){
                int nr = row + dict[i][0];
                int nc = col + dict[i][1];
                if(nr >= 0 && nr < grid.size() && nc >= 0 && nc < grid[0].size()){
                    //nr, nc is within the boundary
                    if(grid[nr][nc] == '1'){
                        DFS(grid, nr, nc);
                    }
                }
        }
    }
};

Untitled

 

BFS approach

// BFS Approach
class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        queue<pair<int, int>> q;
        if(grid.size() == 0 || grid[0].size() == 0) return 0;
        int count = 0;
        int dict[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        for(int i = 0; i < grid.size(); i++){
            for(int j = 0; j < grid[0].size(); j++){
                if(grid[i][j] == '1'){
                    // BFS
                    count++;
                    q.push(make_pair(i, j));
                    grid[i][j] = '2'; //visited
                    while(!q.empty()){
                        int r = q.front().first;
                        int c = q.front().second;
                        q.pop();
                        for(int k = 0; k < 4; k++){
                            int nr = r + dict[k][0];
                            int nc = c + dict[k][1];
                            if(nr < 0 || nc < 0 || nr >= grid.size() || nc >= grid[0].size() || grid[nr][nc] != '1'){
                                continue;
                            }
                            q.push(make_pair(nr, nc));
                            grid[nr][nc] = '2'; // visited
                        }
                    }
                }
            }
        }
        return count;
    }
};

Union find approach

// Union find approach
class Solution {
public:
    vector<int> unionset;
    int numIslands(vector<vector<char>>& grid) {
        unordered_set<int> islands;
        int height = grid.size();
        if(height == 0) return 0;
        int width = grid[0].size();
        if(width == 0) return 0;
        int dict[][2] = {{0, -1}, {-1, 0}};
        // init unionset
        unionset = vector<int>(height * width, 0);
        for(int i = 0; i < unionset.size(); i++){
            unionset[i] = i;
        }
        
        for(int i = 0; i < height; i++){
            for(int j = 0; j < width; j++){
                if(grid[i][j] == '1'){
                    // only search for left and up neighbours
                    for(int k = 0; k < 2; k++){
                        int nr = i + dict[k][0];
                        int nc = j + dict[k][1];
                        if(nr < 0 || nc < 0) continue;
                        if(grid[nr][nc] == '1'){
                            int root = find(nr * width + nc);
                            if(islands.count(root) > 0) islands.erase(root);
                            set(root, i * width + j);
                        } 
                    }
                    islands.insert(i * width + j);
                }
            }
        }
        return islands.size();
    }
    int find(int idx){
        if(unionset[idx] == idx) return idx;
        else{
            int root = find(unionset[idx]);
            unionset[idx] = root;
            return root;
        }
    }
    void set(int idx, int root){
        unionset[idx] = root;
    }
};

 


Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 thoughts on “[leetcode] Number of Islands