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:
Input: m = 3, n = 3, positions = [[0,0], [0,1], [1,2], [2,1]]
Output: [1,1,2,3]
Explanation:
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 grid0 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 grid0 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 grid1 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 grid2 into a land.
1 1 0
0 0 1 Number of islands = 3
0 1 0
Follow up:
Can you do it in time complexity O(k log mn), where k is the length of the positions?
Solution
class Solution {
public List<Integer> numIslands2(int m, int n, int[][] positions) {
List<Integer> res = new ArrayList<>();
if (m <= 0 || n <= 0) return res;
int[] parents = new int[m*n];
//fill parents[] with -1 as parents[i] would be in [0, m*n-1];
Arrays.fill(parents, -1);
//directions used for calculating positions of 4 neighbors
int[][] directions = {{-1,0},{1,0},{0,-1},{0,1}};
//count varies with each operation (iterating positions)
int count = 0;
//iterate each operation (adding island on certain position)
for (int[] position: positions) {
//2D converted to 1D --> point = n*x+y, save to island
int island = n*position[0]+position[1];
//the parent is the island itself
parents[island] = island;
//after added the new island, increase count
count++;
for (int[] direction: directions) {
//calculate each neighbor --> 2D to 1D, save to neighbor
int x = position[0]+direction[0];
int y = position[1]+direction[1];
int neighbor = n*x+y;
//continue if neighbor position invalid or neighbor ancestor remains -1 (no island)
if (x < 0 || y < 0 || x >= m || y >= n || parents[neighbor] == -1) continue;
//FIND the neighbor ancestor (the existing island)
int neighborParent = findParent(neighbor, parents);
//when neighbor`s island is a different island, do UNION, and reduce count
if (neighborParent != island) {
parents[island] = neighborParent;
island = neighborParent;
count--;
}
}
//operation ended, add count of current operation into result list
res.add(count);
}
return res;
}
private int findParent(int island, int[] parents) {
//find the ancestor by calling findParent() recursively
if (parents[island] != island) island = findParent(parents[island], parents);
return island;
}
}
Optimization: path compression + union by rank
class Solution {
public List<Integer> numIslands2(int m, int n, int[][] positions) {
List<Integer> res = new ArrayList<>();
if (m <= 0 || n <= 0) return res;
//use size[] array to store union size for ranking
int[] parents = new int[m*n];
int[] size = new int[m*n];
Arrays.fill(parents, -1);
int count = 0;
int[][] dirs = {{0,1},{0,-1},{1,0},{-1,0}};
for (int[] pos: positions) {
int island = pos[0]*n+pos[1];
parents[island] = island;
size[island]++;
count++;
for (int[] dir: dirs) {
int x = pos[0]+dir[0];
int y = pos[1]+dir[1];
int nb = n*x+y;
if (x < 0 || x >= m || y < 0 || y >= n || parents[nb] == -1) continue;
int nbParent = find(parents, nb);
int parent = find(parents, island);
if (nbParent != parent) {
//union-by-rank strategy: smaller union unites to the larger union
if (size[parent] >= size[nbParent]) {
size[parent] += size[nbParent];
parents[nbParent] = parent;
} else {
size[nbParent] += size[parent];
parents[parent] = nbParent;
}
count--;
}
}
res.add(count);
}
return res;
}
private int find(int[] parents, int child) {
if (parents[child] != child) {
//path compression
parents[child] = find(parents, parents[child]);
}
return parents[child];
}
}