首页 > 其他 > 详细

【58】305. Number of Islands II

时间:2017-02-12 13:37:11      阅读:202      评论:0      收藏:0      [点我收藏+]

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;
    }
};

 

【58】305. Number of Islands II

原文:http://www.cnblogs.com/93scarlett/p/6390736.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!