Thinking
Something In Common
Both of two questions input a two-dimensional array, what’s more, them all need find and union operation. If union sucessfully, number of islands or friend-circles minus 1. Something Different
In the beginning, the number of islands equal to the number of ‘1’, however,the number of friend-circles equal to the row number of the array. No.200 needs union surrounding area,while No.547 only union i and j.
Code
class UFset {
public:
UFset(vector<vector<char>>& grid) {
int row = grid.size();
int col = grid[0].size();
islandCnt = 0;
parent = new int[row * col]();
root = new int[row * col ] ();
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
if (grid[i][j] == '1') {
parent[col*i + j] = 1;
islandCnt++;
}
root[col*i + j] = 1;
}
}
}
int uf_find(int x) {
int x_root = x;
while (!root[x_root]) x_root = parent[x_root];
// compress path
while (x_root != x) {
int temp = parent[x];
parent[x] = x_root;
x = temp;
}
return x_root;
}
int uf_union(int x, int y) {
int x_root = uf_find(x);
int y_root = uf_find(y);
if (x_root != y_root) {
if (parent[x_root] < parent[y_root]) {
parent[y_root] += parent[x_root];
parent[x_root] = y_root;
root[x_root] = 0;
} else {
parent[x_root] += parent[y_root];
parent[y_root] = x_root;
root[y_root] = 0;
}
islandCnt--;
return 1;
} else {
return 0;
}
}
int getCnt() {
return islandCnt;
}
private:
int* parent;
int* root;
int islandCnt;
};
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int row = grid.size();
if (!row) return 0;
int col = grid[0].size();
UFset uf(grid);
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
if (grid[i][j] == '1') {
grid[i][j] == '0';
if (i-1 >= 0 && grid[i-1][j] == '1') uf.uf_union(i*col+j, (i-1)*col+j);
if (i+1 < row && grid[i+1][j] == '1') uf.uf_union(i*col+j, (i+1)*col+j);
if (j-1 >= 0 && grid[i][j-1] == '1') uf.uf_union(i*col+j, i*col+j-1);
if (j+1 < col && grid[i][j+1] == '1') uf.uf_union(i*col+j, i*col+j+1);
}
}
}
return uf.getCnt();
}
};
class UFset {
public:
UFset(vector< vector<int> >& M) {
int row = M.size();
int col = M[0].size();
parent = new int[row * col]();
root = new int[row*col]();
friendCircles = row;
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
if (M[i][j] == 1) {
parent[i*col + j] = 1;
}
root[i*col + j] = 1;
}
}
}
int uf_find(int x) {
int x_root = x;
while (!root[x_root]) {
x_root = parent[x_root];
}
while (x_root != x) {
int temp = parent[x];
parent[x] = x_root;
x = temp;
}
return x_root;
}
int uf_union(int x, int y) {
int x_root = uf_find(x);
int y_root = uf_find(y);
if (x_root != y_root) {
if (parent[x_root] < parent[y_root]) {
parent[y_root] += parent[x_root];
parent[x_root] = y_root;
root[x_root] = 0;
} else {
parent[x_root] += parent[y_root];
parent[y_root] = x_root;
root[y_root] = 0;
}
friendCircles--;
return 1;
} else {
return 0;
}
}
int getCnt() {
return friendCircles;
}
private:
int* parent;
int* root;
int friendCircles;
};
class Solution {
public:
int findCircleNum(vector<vector<int>>& M) {
int row = M.size();
if (!row) return 0;
int col = M[0].size();
UFset uf(M);
for (int i=0; i<row; i++) {
for (int j=i; j<col; j++) {
if (M[i][j] == 1) {
uf.uf_union(i, j);
}
}
}
return uf.getCnt();
}
};