Number of Islands II

Problem:
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 = 3positions = [[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?

Analysis:
Reverse approach of number of island 1.
  1. count + 1 when adds island
  2. check four directions, if not unioned, union and reduce count by 1.

For instance, when adding [1,1], there are 2 island adjacent to [1,1]. So there are 3 islands now. By unioining [1,1] to the adjacent islands, total number of islands becomes 1. 

If [1,1] has been wrapped by one island at 3 direction. We can only union the first available direction and then we know that the other directions share same root. 


Solution:

class Solution {
    public List<Integer> numIslands2(int m, int n, int[][] positions) {
        int count = 0;
        int[] roots = new int[m*n];
        Arrays.fill(roots, -1);
        List<Integer> res = new ArrayList<>();
        for (int[] pos: positions) {
            int[][] dirs = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
            count++;
            int cur = pos[0] * n + pos[1];
            roots[cur] = cur;
            for (int[] dir: dirs) {
                int x = pos[0] + dir[0];
                int y = pos[1] + dir[1];
                int nb = x*n + y;
                
                if (0 <= x && x < m && 0 <= y && y < n && roots[nb]!= -1) {
                    int rootNb = find(roots,nb);
                    int rootCur = find(roots,cur);
                    if (rootNb != rootCur) {
                        roots[rootCur] = rootNb;
                        count--;    
                    }
                    
                }
            }
            res.add(count);
        }

        return res;
    }

    private int find(int[] roots, int n) {
        if (roots[n] != n) {
            roots[n] = find(roots, roots[n]);
        }
        return roots[n];
    }
}

评论

此博客中的热门博文

663. Equal Tree Partition

776. Split BST

426. Convert Binary Search Tree to Sored Doubly Linked List