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 ...
随机推荐
- iOS git 托管代码 常用几个操作
学习 git 切换分支 1 从远程下载一个分支develop(本地没有的) (1) git fetch origin develop (2) git checkout develop (默认 分支切 ...
- Linux Shell编程 条件判断语法
if条件判断语句 单分支 if 条件语句 语法格式: if [条件判断式];then 程序 fi 或者 if [条件判断式] then 程序 fi 在使用单分支 if 条件查询时需要注意几点: if ...
- 【Flask】ORM关系以及一对多
### ORM关系以及一对多:mysql级别的外键,还不够ORM,必须拿到一个表的外键,然后通过这个外键再去另外一张表中查找,这样太麻烦了.SQLAlchemy提供了一个`relationship`, ...
- POI 百万数据导出
poi 导出主类 package test; import java.io.File; import java.io.FileOutputStream; import java.lang.reflec ...
- QGIS 编译
QGIS 编译 在编译的过程中花费了很长时间,特别是编译Debug版本.release版本的编译可以从晚上找到很多的资料,但是Debug的编译相对较少.在Debug编译的过程中,需要单独build工程 ...
- java.net.UnknownHostException异常处理
1.问题描述 最近迁移环境,在Linux系统下部署Java产品的应用,后台报出如下异常,系统报找不到名为“xxx-houtai1”的主机: 1 java.net.UnknownHostExceptio ...
- 【P2158】仪仗队&欧拉函数详解
来一道数论题吧. 这个题一眼看上去思路明确,应该是数论,但是推导公式的时候却出了问题,根本看不出来有什么规律.看了马佬题解明白了这么个规律貌似叫做欧拉函数,于是就去百度学习了一下这东西. 欧拉函数的含 ...
- linux基础(6)-shell编程
shell脚本 shell脚本程序:以文件形式存放批量的linux命令集合,该文件能够被shell释放执行.通常由一段linux命令.shell命令.控制语句以及注释语句构成. shell脚本特点: ...
- WIN7 X64 PASSUAC 源码
// Passuac.cpp : Defines the entry point for the console application. // #include "stdafx.h&quo ...
- VC SOCKET 压缩通信学习
Server................// Server.cpp : Defines the entry point for the console application. // #inclu ...