[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 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
?
200. Number of Islands 变形,这题是一个点一个点的增加,最开始初始化时没有陆地,每增加一个点,都要统一现在总共的岛屿个数。
使用Union-Find对小岛进行合并。并查集记得要进行压缩(islands[island] = islands[islands[island]]; ),速度会快很多。
Disjoint-set data structure(union-find data structure)
Java:
public class Solution {
private int[] islands;
private int root(int island) {
while (islands[island] != island) {
islands[island] = islands[islands[island]];
island = islands[island];
}
return island;
}
private int[] yo = {-1, 1, 0, 0};
private int[] xo = {0, 0, -1, 1};
public List<Integer> numIslands2(int m, int n, int[][] positions) {
islands = new int[m*n];
Arrays.fill(islands, -1);
int island = 0;
List<Integer> nums = new ArrayList<>();
for(int i=0; i<positions.length; i++) {
int y =positions[i][0];
int x = positions[i][1];
int id=y*n+x;
islands[id] = id;
island ++;
for(int j=0; j<4; j++) {
int ny = y+yo[j];
int nx = x+xo[j];
int nid=ny*n+nx;
if (ny>=0 && ny<m && nx>=0 && nx<n && islands[nid] != -1) {
int root = root(nid);
if (root != id) {
islands[root] = id;
island --;
}
}
}
nums.add(island);
}
return nums;
}
}
类似题目:
[LeetCode] 200. Number of Islands 岛屿的数量
[LeetCode] 323. Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
[LeetCode] 261. Graph Valid Tree 图是否是树
All LeetCode Questions List 题目汇总
[LeetCode] 305. Number of Islands II 岛屿的数量 II的更多相关文章
- [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 ...
- LeetCode 305. Number of Islands II
原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...
- [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] 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] 0200. Number of Islands 岛屿的个数
题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is su ...
- LeetCode 200. Number of Islands 岛屿数量(C++/Java)
题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...
- 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 用了第一种方式, ...
随机推荐
- 摘:Windows系统内存计数器理解解析_备忘录_51Testing软件测试网...
[原创]Windows系统内存计数器理解解析 2008-05-13 11:42:23 / 个人分类:性能测试 说明:本文的计数器以Windows2003为准. 序言;F9n)\%V1a6Z C)?ZV ...
- Wideband Direction of Arrival Estimation Based on Multiple Virtual Extension Arrays
基于多重虚拟扩展阵列的宽带信号DOA估计[1]. 宽带DOA估计是阵列信号处理领域的一个重要研究方向.在DOAs估计的实际应用中,信号总是会被噪声破坏,在某些情况下,源信号的数量大于传感器的数量,因此 ...
- 思科ASA对象组NAT
ACL对象组NAT配置 ciscoasa#conf t ciscoasa(config)#hostname ASA ASA(config)#domain-name asa.com ASA(config ...
- WinDbg常用命令系列---!cppexr
!cppexr 简介 !cppexr显示C++异常记录的内容. 使用形式 !cppexr Address 参数 Address指定要显示的C++异常记录的地址. 支持环境 Windows 2000 E ...
- WinDbg常用命令系列---单步执行p*
p (Step) p命令执行单个指令或源代码行,并可选地显示所有寄存器和标志的结果值.当子例程调用或中断发生时,它们被视为单个步骤. 用户模式: [~Thread] p[r] [= StartAddr ...
- rust cargo 一些方便的三方cargo 子命令扩展
内容来自cargo 的github wiki,记录下,方便使用 可选的列表 cargo-audit - Audit Cargo.lock for crates with security vulner ...
- linux命令之------Chmod命令
Chmod命令 1)作用:linux和unix的文件调用权限分为三级:文件拥有者/群组/其他.利用chmod可以控制文件如何被他人所调用.(主要就是修改文件夹,文件的权限) 2)U表示该文件的拥有者, ...
- HEXO快速搭建自己的博客
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 很多人有自己的博客,那么你想要吗?利用Hexo就可以搭建专属自己 ...
- MySQL的ROUND函数
ROUND(X) ROUND(X,D) 返回参数X, 其值接近于最近似的整数.在有两个参数的情况下,返回 X ,其值保留到小数点后D位,而第D位的保留方式为四舍五入.若要接保留X值小数点左边的D 位, ...
- java 优秀开源项目
一.https://github.com/zhangdaiscott/jeecg-boot 简介:一款基于代码生成器的JAVA快速开发平台!全新架构前后端分离:SpringBoot 2.x,Ant D ...