LeetCode 305. Number of Islands II
原题链接在这里: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 = 3, positions = [[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 Islands, Number of Connected Components in an Undirected Graph.
LeetCode 305. Number of Islands II的更多相关文章
- [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 ... 
- [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 ... 
- 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 ... 
- [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 ... 
- 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 用了第一种方式, ... 
- [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 ... 
- 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 ... 
- [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 ... 
- 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 ... 
随机推荐
- hadoop  linux  杂记
			切换到root su 修改sudo sudo + 命令 --> root权限 + 命令 su root vim /etc/sudoers ... 
- HackerRank - maximum-perimeter-triangle 【水】
			题意 给出一系列数字,判断其中哪三个数字可以构成一个三角形,如果有多个,输出周长最大的那个,如果没有输出 - 1 思路 数据较小,所有情况FOR一遍 判断一下 AC代码 #include <cs ... 
- C51数据类型
- openGL学习进程(1)OpenGL的简介
			通过本节,我们来简要了解一下openGL. (1)OpenGL概述: openGL(Open Graphics Library)是个专业的图形程序接口,定义了一个跨编程语言.跨平台的编 ... 
- Linux权限管理 ACL权限
			ACL权限简介 在普通权限中,用户对文件只有三种身份ugo,分别为属主(u).属组(g)和其他人(o):每种用户身份拥有读(read).写(write)和执行(execute)三种权限.但是在实际工作 ... 
- css li 间隙
			如果 li 未浮动,而 li 子元素浮动,则ie6和ie7下会出现间隙,解决办法是给 li 写上css hack *vertical-align:bottom; 
- C#多线程学习之:Monitor类
			关于对C#多线程类Monitor的理解 1.对线程的理解 围绕着锁周围的线程可以分为以下三类: l 拥有锁的线程:只有一个 l 就绪队列:只有就绪队列里的线程才有机会在锁被释放时去获取锁. l ... 
- vector对象
			vector是模板而非类型,由vector生成的类型必须包含vector中元素的类型,例如vector<int> 定义和初始化vector对象: vector<T> v1 ... 
- kafka原理学习好文
			摘自:http://blog.csdn.net/suifeng3051/article/details/48053965 http://blog.csdn.net/ychenfeng/article/ ... 
- 使用标签代替goto关键字
			众所周知,java中没有goto语句,但是保留了goto这个关键字.由于goto是在源码级上的跳转,多次使用goto会引起代码混乱容易出错,这也是java取消goto语句的目的所在,但是goto语句也 ... 
