[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 ...
随机推荐
- spring MVC注解深入研究
@Controller @Service @Controller和 @Component注册一个action 到spring 上下文中,bean 的ID 默认为类名称开头字母小写.@Reposito ...
- Hadoop:Windows 7 32 Bit 编译与运行
所需工具 1.Windows 7 32 Bit OS(你懂的) 2.Apache Hadoop 2.2.0-bin(hadoop-2.2.0.tar.gz) 3.Apache Hadoop 2.2.0 ...
- clearing & settlement
http://blog.donews.com/tianshun/archive/2013/07/ http://wenku.baidu.com/view/e5a736e3e53a580217fcfe1 ...
- js时间函数
时间 new Date() 获取时间,可以理解为是系统默认的函数. 从小括号里面获取系统时间日期,相当于在调用系统默认的函数. 年 getFullYear() 注意,有Full. 月 getMonth ...
- Kafka重复消费和丢失数据研究
Kafka重复消费原因 底层根本原因:已经消费了数据,但是offset没提交. 原因1:强行kill线程,导致消费后的数据,offset没有提交. 原因2:设置offset为自动提交,关闭kafka时 ...
- Inno Setup使用技巧
一.关于Inno Setup如何在安装时播放音乐 方法(1): 在脚本编译里的[Code]与[Files]段处添加以下代码: [Code] Function mciSendString(lpszCom ...
- C#下取得Exif中照片拍摄日期
/// <summary> /// 获取Exif中的照片拍摄日期 /// </summary> /// <param name="fileName"& ...
- 强烈推荐android studio用的几个插件
http://blog.csdn.net/liang5630/article/details/46366901 android studio常用插件,可极大简化开发,增强开发效率. 不懂安装studi ...
- 通过修改host文件来允许和禁止主机的访问
通过修改host文件来允许和禁止主机的访问 修改/etc/hosts.deny,加入"sshd:ALL" 修改/etc/hosts.allow,加入"sshd:192.1 ...
- python coroutine测试
目的:实现一个类似于asyn await的用法,来方便的编写callback相关函数 from __future__ import print_functionimport timeimport th ...