[LeetCode] Number of Islands II
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 = 3
, positions = [[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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- LeetCode 305. Number of Islands II
原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- ZooKeeper 3.5.0 分布式配置问题
ZooKeeper 3.5.0 分布式配置好后,执行./zkServer.sh start 命令启动,报如下错误: 2015-07-02 21:06:01,671 [myid:] - INFO [ma ...
- GCC选项
-g: Debugging Option. 提供给GDB的debugging信息的选项: -fno-omit-frame-pointer: Optimization Option: -Wstrict- ...
- MMM互助金融/理财源码
1.1.1MMM互助金融配比系统源码销售 (3mphp.com/mmm-office.com) 联系QQ: 3375551869,全套源码,包含: 1 源码:安装.开发文档 2 数据库:含演示数据,自 ...
- 使用UEditor无法SetContent的问题
无法SetContent是因为 <script id="txtContent" name="txtContent" type="text/pla ...
- SQL Server FileStream
以往我们对文件管理有两种方法: 数据库只保存文件的路径,具体的文件保存在文件服务器(NFS)上,使用时,编程实现从文件服务器读取文件: 将文件直接以varbinary(max)或image数据类型保存 ...
- nosql/nodejs基础
nosql定义:nosql--no only sql 目前流行的非关系型数据库:mongodb,redis,cassandra 非关系型数据库和内存存储hashmap数据结构有什么区别?hashmap ...
- [2]. jekyll安装与应用
一.ruby安装 这里在win下安装ruby,对应自己电脑的操作系统位数,如我的是64位,对应下载Ruby 2.0.0-p353 (x64)这个版本的ruby.然后安装过程就很简单了: 命令行下输入r ...
- [Java拾遗一] XML的书写规范与解析.
前言今天天气大好, 起了个大早开始总结一些常用的基础知识. XML一直来说都很陌生, 使用大多是用于配置文件, 之前并没有细究过其中的约束规范, 今天刚好没事来学习并总结下. 1,XML基础介绍 XM ...
- atitit.元编程总结 o99
atitit.元编程总结 o99.doc 1. 元编程(Metaprogramming) 1 2. 元编程的历史and发展 1 3. 元类型and元数据 1 4. 元编程实现方式 2 4.1. 代码生 ...
- paip.基于urlrewrite的反向代理以及内容改写
paip.基于urlrewrite的反向代理以及内容改写 ---------反向代理 RewriteCond %{REQUEST_URI} !=/process.php RewriteRule ^( ...