原题链接在这里: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 = 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?

题解:

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

为什么Union Find 的parent size 是m*n+1 而不是m*n呢? 因为parent[i] = 0时默认没有parent, 若index从0开始, 那么parent[i] = 0时无法判别是没有parent, 还是parent是0.

Note: there could be duplicate in the positions.

Time Complexity: O(k*logmn). k = edges.length. Find: O(logmn).

Space: O(mn).

AC Java:

 class Solution {
int [] parent;
int [] size;
int count; public List<Integer> numIslands2(int m, int n, int[][] positions) {
List<Integer> res = new ArrayList<>();
if(positions == null || positions.length == 0){
return res;
} parent = new int[m * n + 1];
size = new int[m * n + 1];
count = 0; int [][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; for(int [] p : positions){
int ind1 = p[0] * n + p[1] + 1;
if(parent[ind1] != 0){
res.add(count);
continue;
} parent[ind1] = ind1;
size[ind1] = 1;
count++; for(int [] dir : dirs){
int x = p[0] + dir[0];
int y = p[1] + dir[1];
if(x < 0 || x >= m || y < 0 || y >= n){
continue;
} int ind2 = x * n + y + 1;
if(parent[ind2] > 0 && !find(ind1, ind2)){
union(ind1, ind2);
}
} res.add(count);
} return res;
} private boolean find(int i, int j){
return root(i) == root(j);
} private int root(int i){
while(i != parent[i]){
parent[i] = parent[parent[i]];
i = parent[i];
} return parent[i];
} private void union(int i, int j){
int p = root(i);
int q = root(j);
if(size[p] > size[q]){
size[p] += size[q];
parent[q] = p;
}else{
size[q] += size[p];
parent[p] = q;
} count--;
}
}

类似Number of IslandsNumber of Connected Components in an Undirected Graph.

LeetCode 305. Number of Islands II的更多相关文章

  1. [LeetCode] 305. Number of Islands II 岛屿的数量 II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  2. [LeetCode] 305. Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  3. 305. Number of Islands II

    题目: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand  ...

  4. [LeetCode] 200. Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  5. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  6. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  7. Leetcode: Number of Islands II && Summary of Union Find

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  8. [LeetCode] Number of Islands II

    Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...

  9. LeetCode – Number of Islands II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

随机推荐

  1. 使用新浪IP库获取IP详细地址

    使用新浪IP库获取IP详细地址 <?php class Tool{ /** * 获取IP的归属地( 新浪IP库 ) * * @param $ip String IP地址:112.65.102.1 ...

  2. HTML系列(2)基本的HTML标签(一)

        本节介绍基本的HTML标签的使用实例.     (1)h标签: <!DOCTYPE html> <html> <head> <title>示例2 ...

  3. matlab 读取nc

    在这里做个记录,这几个是matlab用来读取.nc格式数据的函数.只是函数,参数和变量为了便于理解,取括号中的名字.   fid=netcdf.open('fname','nowriter');%打开 ...

  4. token的生成和应用

    token的生成和应用 接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效: 2.因为是非开放性的,所以OAuth那套协议是行不通的,因为没有中间用户的授权过程: ...

  5. sql中in和exists的区别效率问题 转

    in 和exists in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询. 一直以来认为exists 比in 效率高的说法是不准确的.如果 ...

  6. 关于Class.getResourceAsStream

    Properties properties = new Properties();    properties.load(new  InputStreamReader(CharactorTest.cl ...

  7. Python自然语言处理 - 系列三

    有监督分类过程 ![enter image description here][1]例子:涉及一个特征器,给定一个姓名分析出是男性名字还是女性名字 分析:男性和女性的名字有一些鲜明的特点.以a,e 和 ...

  8. 定制AIX操作系统的shell环境

    操作系统与外部最主要的接口就叫做shell.shell是操作系统最外面的一层.shell管理你与操作系统之间的交互:等待你输入,向操作系统解释你的输入,并且处理各种各样的操作系统的输出结果. shel ...

  9. busybox rmmod error — rmmod: chdir(2.6.25): No such file or directory

    busybox rmmod error rmmod: chdir(2.6.25): No such file or directory 1. install your modules in dir / ...

  10. Too many open files 问题

    1.解决办法 (1)查看 查看当前系统打开的文件数量 lsof | wc -l watch "lsof | wc -l" 查看某一进程的打开文件数量 lsof -p pid | w ...