[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 用了第一种方式, ...
随机推荐
- Python+Appium学习之启动手机APP或者浏览器
一.启动浏览器:pycharm中python脚本如下: from appium import webdriver desired_caps ={ 'platformName':'Android', ' ...
- 算法图解(python3版本)--读后感
本想写详细点,但入门书籍没啥干货,一天就看完了,简单介绍下: 大纲--两方面 一.介绍算法是什么:算法的作用,判断算法效率高低的指标 ①通过编程解决问题的思路,或者说程序本身就是算法,算法作用是为了提 ...
- IGC(Interleaved Group Convolutions)
深度学习被引起关注是在2012年,用神经网络训练的一个分类模型在ImagNet上取得了第一名,而且其分类精度比第二名高出10多个点,当时所使用的模型为AlexNet,现在看来其为一个比较简单的网络,而 ...
- [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)
题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0(n) be the number of positive diviso ...
- LeetCode 742. Closest Leaf in a Binary Tree
原题链接在这里:https://leetcode.com/problems/closest-leaf-in-a-binary-tree/ 题目: Given a binary tree where e ...
- ssh集成
导入pom依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
- 洛谷P2744 量取牛奶
题目 DP或者迭代加深搜索,比较考验递归的搜索. 题目第一问可以用迭代加深搜索限制层数. 第二问需要满足字典序最小,所以我们可以在搜索的时候把比当前答案字典序大的情况剪枝掉. 然后考虑怎么搜索,对于每 ...
- tomcat9源码导入idea
maven部署 下载源码 tomcat最新版的github地址 tomcat9官网下载 步骤 源码根目录新建 home 文件夹 把 conf 文件夹和 webapps 文件夹移动到 home 文件夹 ...
- Pyton项目打包成exe文件
Python项目打包成exe文件 1 系统环境 windows版本: Win7 64位 python环境:Anaconda python版本:3.6 64位 pyinstaller版本:3.5 1 安 ...
- 你对SQA的职责和工作活动(如软件度量)的理解?
SQA就是独立于软件开发的项目组,通过对软件开发过程的监控,来保证软件的开发流程按照指定的CMM规程(如果有相应的CMM规程),对于不符合项及时提出建议和改进方案,必要时可以向高层经理汇报以求问题的解 ...