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

最新文章

  1. iOS与JS交互实战篇(ObjC版)
  2. PHP 调试用函数
  3. SQL SERVER类型与C#类型对照
  4. Netfilter深度解剖
  5. java中实现多态的机制是什么?
  6. mac install php dev
  7. javaee学习-Cookie使用范例
  8. 合并JS和CSS
  9. ant学习(1)
  10. CentOS和Ubuntu的区别
  11. 监控Informix-Url
  12. 线程高级.md
  13. LeetCode &amp; Q53-Maximum Subarray-Easy &amp; 动态规划思路分析
  14. OpenStack-Storage(6)
  15. iview中,table组件在缩进时产生的bug。
  16. 常量(const)和只读变量(readonly)
  17. 广搜 poj3278 poj1426 poj3126
  18. 带你看懂大数据采集引擎之Flume&amp;采集目录中的日志
  19. Redis 基础操作
  20. lintcode 40. 用栈实现队列

热门文章

  1. loadrunder之脚本篇——action分类
  2. 每天一个Linux命令(53)service命令
  3. 建议37:按需选择sort或sorted
  4. $.queue() 与 $.dequeue() -- 队列
  5. 使用awk来提取内容
  6. Tomcat8内置jdk8运行环境发布web项目
  7. 《机器学习基石》第一周 —— When Can Machine Learn?
  8. centos6 多段Ip添加脚本
  9. 【bzoj5085】最大(二分+乱搞)
  10. mvn库