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).

  1. 0 0 0
  2. 0 0 0
  3. 0 0 0

Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land.

  1. 1 0 0
  2. 0 0 0 Number of islands = 1
  3. 0 0 0

Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land.

  1. 1 1 0
  2. 0 0 0 Number of islands = 1
  3. 0 0 0

Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land.

  1. 1 1 0
  2. 0 0 1 Number of islands = 2
  3. 0 0 0

Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land.

  1. 1 1 0
  2. 0 0 1 Number of islands = 3
  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:

  1. public class Solution {
  2. private int[] islands;
  3. private int root(int island) {
  4. while (islands[island] != island) {
  5. islands[island] = islands[islands[island]];
  6. island = islands[island];
  7. }
  8. return island;
  9. }
  10. private int[] yo = {-1, 1, 0, 0};
  11. private int[] xo = {0, 0, -1, 1};
  12. public List<Integer> numIslands2(int m, int n, int[][] positions) {
  13. islands = new int[m*n];
  14. Arrays.fill(islands, -1);
  15. int island = 0;
  16. List<Integer> nums = new ArrayList<>();
  17. for(int i=0; i<positions.length; i++) {
  18. int y =positions[i][0];
  19. int x = positions[i][1];
  20. int id=y*n+x;
  21. islands[id] = id;
  22. island ++;
  23. for(int j=0; j<4; j++) {
  24. int ny = y+yo[j];
  25. int nx = x+xo[j];
  26. int nid=ny*n+nx;
  27. if (ny>=0 && ny<m && nx>=0 && nx<n && islands[nid] != -1) {
  28. int root = root(nid);
  29. if (root != id) {
  30. islands[root] = id;
  31. island --;
  32. }
  33. }
  34. }
  35. nums.add(island);
  36. }
  37. return nums;
  38. }
  39. }

 

类似题目:

[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的更多相关文章

  1. [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 ...

  2. LeetCode 305. Number of Islands II

    原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...

  3. [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 ...

  4. [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 ...

  5. 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  ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. 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 用了第一种方式, ...

随机推荐

  1. 手写简单的php生成Html网页

    这个是基本功,以前用到laravel及thinkphp时,这一步,都被设置好了吧. 这里只依靠纯的php环境,而没有任何框架, 而框架,只是将这一切规范化,加快代码效率及减小沟通成本,维护升级也方便, ...

  2. qt事件机制(转)

    学习了一段时间的Qt之后,发现Qt的事件机制和其他语言的机制有些不同.Qt除了能够通过信号和槽机制来实现一些Action动作之外,还可以用对象所带的事件,或者用户自定义的事件来实现对象的一些行为处理. ...

  3. DT6.0开发之-调用信息评论

    今天在做destoon6.0产品内页开发时候,用到了调取当前信息评论,所以就顺便做下笔记. 调用当前信息的评论代码: <!--{tag("table=destoot_comment&a ...

  4. CentOS7下安装Python3和PIP3

    为了方便以后编译,所以整合了下配置流程 先查看是否安装了Python3 python3 -V 如果没有安装,则先安装依赖包 yum install zlib-devel bzip2-devel ope ...

  5. python操作json文件获取内容

    写case时,将case 写到json文件比写到,写python一定要学会处理json 以下,是要处理的json 处理操作包括:打开json文件,获取json文件内容,关闭json文件,读取内容中的对 ...

  6. JS 不声明第三个变量的情况下实现两数变换

    1. var a = 1; var b = 2; a = a + b; b = a - b; a = a - b; console.log(a); console.log(b); 2. var a = ...

  7. (尚019)Vue基于脚手架编写项目

    vue_demo目录结构截图: (1)图一 (2).图二 (3).图三 (四).图四 (5).图五 (6).图六 (7).图七 不能随便改入口文件的名字,因为已经配置好了 (8).图八 (9).图九 ...

  8. java 数组遍历(方法内部的代码)

    //数组遍历(依次输出数组中的每一个元素)二维数组: int[][] arr={{1,2},{3,4,5},{6,7}}; for(int i=0;i<arr.length;i++){ for( ...

  9. redash oracle 数据源docker 镜像

    redash 官方的docker 镜像是没有包含oracle的,需要我们自己添加,参考了一个docker 镜像进行了简单的修改 Dockerfile FROM redash/redash:7.0.0. ...

  10. JMX脚本在某些机器上报错,有的运行超时

    运行超时的 是因为在server端运行命令执行脚本,是server给agent下达的指定,但是server端到agent的10050端口没开,所以或一致堵死在那,知道执行超时, 解决:开通server ...