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?

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

  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. python测试开发django-rest-framework-65.序列化(ModelSerializer)

    前言 serializers.Serializer可以对modle模型中的字段序列化,并且必须写create和update两个方法.ModelSerializer可以看成是Serializer的一个升 ...

  2. C# 验证控件组

    C# 验证控件允许使用ValidationGroup给验证控件分组,分组后的两组验证控件可以独立使用,互不相干.比如一个页面有登录和注册两个部分,假如使用验证控件组,提交的时候会对所有的验证控件进行验 ...

  3. python开发笔记-类

    类的基本概念: 问题空间:问题空间是问题解决者对一个问题所达到的全部认识状态,它是由问题解决者利用问题所包含的信息和已贮存的信息主动的地构成的. 初始状态:一开始时的不完全的信息或令人不满意的状况: ...

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

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

  5. (转)python自动化测试之异常及日志

    为了保持自动化测试用例的健壮性,异常的捕获及处理,日志的记录对掌握自动化测试执行情况尤为重要,这里便详细的介绍下在自动化测试中使用到的异常及日志,并介绍其详细的用法. 一.日志 打印日志是很多程序的重 ...

  6. Python语言程序设计(3)--字符串类型及操作--time库进度条

    1.字符串类型的表示: 三引号可做注释,注释其实也是字符串 2.字符串的操作符 3.字符串处理函数 输出:

  7. pandas 常用方法使用示例

    from pandas import DataFrame import numpy as np import pandas as pd t={ , , np.nan, , np.nan, ], &qu ...

  8. 自定义express中间件

    const http = require('http') class LikeExpress { constructor() { this.middleList = [] this.routes = ...

  9. 关于System.AccessViolationException异常

    什么是AccessViolationException 试图读写受保护内存时引发的异常. 继承 Object Exception SystemException AccessViolationExce ...

  10. CLR 调试体系结构

    公共语言运行时 (CLR) 调试 API 专门用作操作系统内核的一部分. 在非托管代码中,当程序生成异常时,内核将暂停执行进程,并使用 Win32 调试 API 将异常信息传递给调试器. CLR 调试 ...