Skip to content

Instantly share code, notes, and snippets.

@cangoal
Last active April 30, 2018 00:02
Show Gist options
  • Select an option

  • Save cangoal/9912100f7e145a7bef2510c5239ad19d to your computer and use it in GitHub Desktop.

Select an option

Save cangoal/9912100f7e145a7bef2510c5239ad19d to your computer and use it in GitHub Desktop.
LeetCode - Number of Islands II
// 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 List<Integer> numIslands2(int m, int n, int[][] positions) {
List<Integer> res = new ArrayList<Integer>();
if(positions == null || positions.length == 0 || positions[0].length == 0) return res;
int count = 0, islandCode = -2;
int[][] map = new int[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
map[i][j] = -1;
}
}
for(int i = 0; i < positions.length; i++){
int x = positions[i][0];
int y = positions[i][1];
count++;
int parent = find(map, x, y);
int copyParent = parent;
for(int[] index : indexes){
if(x + index[0] < 0 || y + index[1] < 0 || x + index[0] >= map.length || y + index[1] >= map[0].length
|| map[x + index[0]][y + index[1]] == -1)
continue;
int parent_adj = find(map, x + index[0], y + index[1]);
if(parent_adj != parent){
if(map[x][y] == -1){
map[x][y] = parent_adj;
parent = parent_adj;
} else {
map[parent_adj / n][parent_adj % n] = parent;
}
count--;
}
}
if(map[x][y] == -1) map[x][y] = islandCode--;
res.add(count);
}
return res;
}
final int[][] indexes = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
private int find(int[][] map, int x, int y){
int n = map[0].length;
if(map[x][y] <= -1) return x * n + y;
int i = map[x][y] / n, j = map[x][y] % n;
return find(map, i, j);
}
// 1-D array
public List<Integer> numIslands2(int m, int n, int[][] positions) {
List<Integer> res = new ArrayList<Integer>();
if(positions == null || positions.length == 0 || positions[0].length == 0) return res;
int count = 0;
int[] map = new int[m * n];
Arrays.fill(map, -1);
for(int k = 0; k < positions.length; k++){
int x = positions[k][0];
int y = positions[k][1];
int parent = x * n + y;
map[parent] = parent;
count++;
for(int[] index : indexes){
int i = x + index[0], j = y + index[1];
if(i < 0 || i >= m || j < 0 || j >= n || map[i * n + j] == -1) continue;
int parentNb = find(map, i * n + j);
if(parent != parentNb){
map[parent] = parentNb;
parent = parentNb;
count--;
}
}
res.add(count);
}
return res;
}
final int[][] indexes = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
private int find(int[] map, int val){
if(map[val] == val) return val;
return find(map, map[val]);
}
// https://leetcode.com/discuss/69392/python-clear-solution-unionfind-class-weighting-compression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment