public class Solution {
class UnionFind {
private int count;
private Map<Integer, Integer> f;
UnionFind(int n) {
this.count = 0;
this.f = new HashMap<Integer, Integer>();
for (int i = 0; i < n; i++) {
f.put(i, i);
}
}
int find(int x){
int root = x;
while (f.get(root) != root) {
root = f.get(root);
}
while (x != root) {
int nx = f.get(x);
f.put(x, root);
x = nx;
}
return root;
}
void union(int a, int b) {
int rootA = find(a);
int rootB = find(b);
if (rootA != rootB) {
this.f.put(rootB, rootA);
this.count--;
}
}
int getCount() {
return this.count;
}
void setCount(int count) {
this.count = count;
}
}
public List<Integer> numIslands2(int n, int m, Point[] operators) {
// write your code here
List<Integer> result = new ArrayList<>();
if (operators == null || operators.length == 0) {
return result;
}
int[] deltaX = new int[]{-1, 0, 1, 0};
int[] deltaY = new int[]{0, 1, 0, -1};
int count = 0;
int[][] island = new int[n][m];
UnionFind unionFind = new UnionFind(n * m);
for (Point operator : operators) {
if (island[operator.x][operator.y] == 1) {
result.add(count);
continue;
}
island[operator.x][operator.y] = 1;
count++;
unionFind.setCount(count);
for (int i = 0; i < 4; i++) {
int nx = operator.x + deltaX[i];
int ny = operator.y + deltaY[i];
if (nx >= 0 && nx < n &&
ny >= 0 && ny < m &&
island[nx][ny] == 1) {
unionFind.union(mapReduce(operator.x, operator.y, m), mapReduce(nx, ny, m));
}
}
count = unionFind.getCount();
result.add(count);
}
return result;
}
private int mapReduce(int x, int y, int m) {
return x * m + y;
}
}
Comments
Post a Comment