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. 更改DHCP服务器默认日志存储位置

    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一种有效的IP 地址分配手段,已经广泛地应用于各种局域网管理.它能动态地向网络中每台计算机分配唯一 ...

  2. 0031ActiveMQ的下载安装与启动

    消息中间件activemq的作用主要是解耦.异步.削峰. 我们按如下步骤详细讲解一下activemq的下载.安装与启动. 1.activemq的下载 下载地址: http://activemq.apa ...

  3. 项目alpha冲刺-总结

    作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Alpha冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及可视化分析平台 这个作业的 ...

  4. delete properties inside object

  5. easyui 自己写的一些小东西

    1设置combobox,当我们只需要显示一个commbobox的时候,并且默认选择第一项 function Getcombobox(comboId, value, groupNo) { $('#' + ...

  6. java通过JDBC连接Oracle并调用存储过程和存储方法

    初始配置:电脑安装oracle 11g(这里也可使是其它版本也可,此教程演示为11g),java环境,eclipse,oracle关于jdbc的jar包. 一,在scott用户下首先要有存储过程和存储 ...

  7. YII2 使用curl请求,返回false

    一. 起因: 今天用yii框架,请求java接口,始终返回false. 二. 分析历程: 使用curl_error()方法打印出Peer’s Certificate issuer is not rec ...

  8. [BZOJ 5127][Lydsy1712月赛]数据校验

    Description 题库链接 给你一个长度为 \(n\) 的序列.\(m\) 次询问,每次询问序列的一个区间 \([l,r]\).对于 \([l,r]\) 内的所有子区间,询问值域是否连续.若存在 ...

  9. H5利用formData来上传文件(包括图片,doc,pdf等各种格式)方法小结!

    H5页面中我们常需要进行文件上传,那么怎么来实现这个功能呢??? 我主要谈如下两种方法. (一).传统的form表单方法 <form action="/Home/SaveFile1&q ...

  10. 【CPLEX教程02】配置Cplex的Java环境以及API说明

    00 前言 因为小编一般用的C++和Java比较多,而且现在开发大型算法用这类面向对象的编程语言也方便得多.基于上面的种种考虑,加上时间和精力有限,所以就暂时只做C++和Java的详细教程辣.关于ma ...