Skip to content

Number of Islands II

Tim_Gao edited this page Sep 17, 2016 · 1 revision

A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. 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:

Given m = 3, n = 3, positions = 0,0], [0,1], [1,2], [2,1. Initially, the 2d grid grid is filled with water. (Assume 0 represents water and 1 represents land).

0 0 0 0 0 0 0 0 0

Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land.

1 0 0 0 0 0 Number of islands = 1 0 0 0

Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land.

1 1 0 0 0 0 Number of islands = 1 0 0 0

Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land.

1 1 0 0 0 1 Number of islands = 2 0 0 0

Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land.

1 1 0 0 0 1 Number of islands = 3 0 1 0

We return the result as an array: [1, 1, 2, 3]

Challenge:

Can you do it in time complexity O(k log mn), where k is the length of the positions?

public class Solution {
    private int numOfIslands = 0;
    private void addIsland(int m, int n, int x, int y, int[] id){
        int index = x*n + y;
        if (id[index] != -1){
            return;
        }
        ++numOfIslands;
        id[index] = index;
        if(x-1>=0 && id[(x-1)*n+y] != -1){
            if(union(index, (x-1)*n+y, id)){
                --numOfIslands;
            }
        }
        if(x+1<m && id[(x+1)*n+y]!=-1){
            if(union(index, (x+1)*n+y, id)){
                --numOfIslands;
            }
        }
        if (y-1>=0 && id[index-1] != -1){
            if(union(index, index -1, id)){
                --numOfIslands;
            }
        }
        if (y+1<n && id[index+1] != -1){
            if(union(index, index+1, id)){
                --numOfIslands;
            }
        }
    }
    private boolean union(int p1, int p2, int[] id){
        int r1 = find(p1, id);
        int r2 = find(p2, id);
        if(r1==r2){
            return false;
        }
        id[r1] = r2;
        return true;
    }
    private int find(int p, int[] id){
        if(id[p] == -1){
            id[p] = p;
        }
        while(id[p]!=p){
            p = id[p];
        }
        return p;
    }
    public List<Integer> numIslands2(int m, int n, int[][] positions) {
        ArrayList<Integer> res = new ArrayList<>();
        if(positions.length == 0){
            return res;
        }
        int[] id = new int[m*n];
        Arrays.fill(id, -1);
        for(int i=0; i<positions.length; ++i){
            addIsland(m, n, positions[i][0], positions[i][1], id);
            res.add(numOfIslands);
        }
        return res;
    }
}

Clone this wiki locally