Problem Description:

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]


This problem requires a classic data structure called UnionFind. Take some efforts to learn it at first, like using this Princeton's notes offered by peisi. This note is very nicely written. Take some patinece to read through it and you will get a tool that is also helpful in the future :-)

The C++ code is as follows, taking peisi's Java code as an example.

 class UnionFind2D {
public:
UnionFind2D(int m, int n) {
for (int i = ; i < m * n; i++) ids.push_back(-);
for (int i = ; i < m * n; i++) szs.push_back();
M = m, N = n, cnt = ;
}
int index(int x, int y) {
return x * N + y;
}
int size(void) {
return cnt;
}
int id(int x, int y) {
if (x >= && x < M && y >= && y < N)
return ids[index(x, y)];
return -;
}
int add(int x, int y) {
int idx = index(x, y);
ids[idx] = idx;
szs[idx] = ;
cnt++;
return idx;
}
int root(int i) {
for (; i != ids[i]; i = ids[i])
ids[i] = ids[ids[i]];
return i;
}
bool find(int p, int q) {
return root(p) == root(q);
}
void unite(int p, int q) {
int i = root(p), j = root(q);
if (szs[i] > szs[j]) swap(i, j);
ids[i] = j;
szs[j] += szs[i];
cnt--;
}
private:
vector<int> ids, szs;
int M, N, cnt;
}; class Solution {
public:
vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
UnionFind2D islands(m, n);
vector<pair<int, int>> dirs = { { , - }, { , }, { -, }, { , } };
vector<int> ans;
for (auto& pos : positions) {
int x = pos.first, y = pos.second;
int p = islands.add(x, y);
for (auto& d : dirs) {
int q = islands.id(x + d.first, y + d.second);
if (q >= && !islands.find(p, q))
islands.unite(p, q);
}
ans.push_back(islands.size());
}
return ans;
}
};

[LeetCode] Number of Islands II的更多相关文章

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

  2. Leetcode: Number of Islands II && Summary of Union Find

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

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

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

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

  6. LeetCode 305. Number of Islands II

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

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

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

  9. [Swift]LeetCode305. 岛屿的个数 II $ 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 ...

随机推荐

  1. 支持事件穿透?使用pointer-events样式

    使用绝对定位元素,让元素A完全盖住元素B时,如何通过元素A来响应元素B的事件呢? 上图可以用下面的SVG代码来实现: <svg width="200" height=&quo ...

  2. C++ Primer 变量和基本类型

    <C++ Primer 4th>读书摘要 基本上所有的语言都要提供下列特征: • 内置数据类型,如整型.字符型等. • 表达式和语句:表达式和语句用于操纵上述类型的值. • 变量:程序员可 ...

  3. Jetty 9嵌入式开发

    官方网址:http://www.eclipse.org/jetty/ 下载地址:http://download.eclipse.org/jetty/stable-9/dist/ 文档网址:http:/ ...

  4. 基于javascript实现图片懒加载(亲测有效)

    这篇文章主要介绍了javascript实现图片懒加载的方法及思路,有时我们需要用懒加载,也就是延迟加载图片的方式,来提高网站的亲和力,需要的朋友可以参考下! 一.定义 图片延迟加载也称为懒加载,延迟加 ...

  5. atitit js 开发工具 ide的代码结构显示(func list) outline总结

    atitit js 开发工具 ide的代码结构显示(func list) outline总结 eclips环境::4.3.1 #-------需要一个js开发工具,可以显示outline或者代码结构显 ...

  6. 数据类型/强制类型转换 和运算符---标识符规则/关键字 a++和++a区别

    3.2关键字都是小写,TRUE FALSE NULL都不是Java关键字 3.3数据类型 变量相当于一个有名称的容器,该容器用于装各种不同类型的数据 Java类型分为2种 基本类型: 引用类型: 基本 ...

  7. HTML5骨骼动画Demo | 使用min2d、createjs、pixi播放spine动画

    Spine做骨骼动画是比较流行的,使用起来可能相对复杂,但功能毕竟强大,所以市场占有率较大. 在unity.cocos2d.starling中使用spine已经很成熟了,而HTML5这一块可能刚刚起步 ...

  8. Delphi XE8,C++ Builder XE8,RAD Studio XE8 官方 ISO 文件下载,附激活工具

    RAD Studio XE8 v22.0.19027.8951 官方ISO下载(6.72G):http://altd.embarcadero.com/download/radstudio/xe8/de ...

  9. 如何在安装程序中判断操作系统是否是64位 inno

    [Setup]; 开启64位模式ArchitecturesInstallIn64BitMode=x64 [Run] ;根据是否是64位进行不同的操作Filename: "..."; ...

  10. STL的string和wstring

    STL有字符串处理类——stirng和wstring,但是用的时候会觉得不是很方便,因为它不能像TCHAR一样根据定义的宏在char类型字符串和wchar_t进行转换,总不能因为程序要Unicode就 ...