【58】305. Number of Islands II

305. Number of Islands II

Description Submission Solutions Add to List

  • Total Accepted: 18274
  • Total Submissions: 47614
  • Difficulty: Hard
  • Contributors: Admin

 

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 = 3positions = [[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?

 

Solution:并查集 Find&Union

class Solution {
public:
    int find(int x){
        if(f[x] == x){
            return x;
        }
        return f[x] = find(f[x]);
    }
    bool merge(int x, int y) {
        if (f[x] == -1 || f[y] == -1)
            return false;
        
         x = find(x);
         y = find(y);
        if(x != y){
            f[x] = y;
            return true;
        }
        return false;
    }
    bool isValid(int x, int y, int m, int n){
        if(x < 0 || x >= m || y < 0 || y >= n){
            return false;
        }
        return true;
    }
    int f[10000];
    vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
        vector<int> res;
        int dir[][2] = {0, 1, 0, -1, 1, 0, -1, 0};
        for(int i = 0; i < m * n; i++){
            f[i] = -1;
        }
        int cnt = positions.size();
        int area = 0;
        for(int i = 0; i < cnt; i++){
            int pos = positions[i].first * n + positions[i].second;
            if(f[pos] == -1){
                area++;
                f[pos] = pos;
            }
            
            for(int j = 0; j < 4; j++){
                int x = dir[j][0] + positions[i].first;
                int y = dir[j][1] + positions[i].second;
                if(isValid(x, y, m, n)){
                    //if(merge(x * positions[i].first + y, pos)){
                    if(merge(x * n + y, pos)){
                        area--;
                    }
                }
            }
            res.push_back(area);
        }
        return res;
    }
};

 

posted @ 2017-02-12 12:30  会咬人的兔子  阅读(105)  评论(0编辑  收藏  举报