您的位置:首页 > 其它

LeetCode Number of Islands II

2016-02-21 09:05 295 查看
原题链接在这里:https://leetcode.com/problems/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
?

Union-Find 题目。是Number of Islands升级版。

一个operation 添加进UnionFind2D型的islands. 看四个方向上islands里parent 对应的值,若是大于0, 就看是不是在一个跟下,若不在,就union起来,同时。四个direction走完,把islands的当前count加入res中。

Time Complexity: O(k*logmn). Space: O(mn).

AC Java:

public class Solution {
int [][] directions = {{0,1},{0,-1},{-1,0},{1,0}};
public List<Integer> numIslands2(int m, int n, int[][] positions) {
List<Integer> res = new ArrayList<Integer>();
if(m == 0 || n == 0 || positions == null || positions.length == 0){
return res;
}

UnionFind2D islands = new UnionFind2D(m, n);
for(int [] row : positions){
int p = islands.add(row[0],row[1]);
for(int [] dir : directions){
int x = row[0] + dir[0];
int y = row[1] + dir[1];
int q = islands.getParent(x,y);
if(q > 0 && !islands.find(p,q)){
islands.union(p,q);
}
}
res.add(islands.size());
}
return res;
}
}

class UnionFind2D{
private int [] parent;
private int [] size;
private int count, m, n;

public UnionFind2D(int m, int n){
this.m = m;
this.n = n;
this.count = 0;
parent = new int[m*n+1];
size = new int[m*n+1];
}

private int getIndex(int i, int j){
return i*n + j + 1;
}

public int add(int i, int j){
int index = getIndex(i,j);
count++;
parent[index] = index;
size[index] = 1;
return index;
}

public int getParent(int i, int j){
if(i < 0 || i>=m || j<0 || j>=n){
return 0;
}
return parent[getIndex(i,j)];
}

public int size(){
return this.count;
}

public boolean find(int p, int q){
return root(p) == root(q);
}

private int root(int i){
if(parent[i] == 0){
return i;
}
while(i != parent[i]){
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}

public void union(int p, int q){
int i = root(p);
int j = root(q);
if(size[i] < size[j]){
parent[i] = j;
size[j] += size[i];
}else{
parent[j] = i;
size[i] += size[j];
}
count--;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: