Number of Islands II
Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operator and each operator has two integer A[i].x, A[i].y means that you can change the grid matrix[A[i].x][A[i].y] from sea to island. Return how many island are there in the matrix after each operator.
0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent.
Example
Example 1:
Input: n = 4, m = 5, A = [[1,1],[0,1],[3,3],[3,4]]
Output: [1,1,2,2]
Explanation:
0. 00000
00000
00000
00000
1. 00000
01000
00000
00000
2. 01000
01000
00000
00000
3. 01000
01000
00000
00010
4. 01000
01000
00000
00011
Example 2:
Input: n = 3, m = 3, A = [[0,0],[0,1],[2,2],[2,1]]
Output: [1,1,2,2]
思路使用并查集,并查集的实现可以利用Point结构体, 也可以将二维坐标转换成一维。注意使用类的时候需重写equals方法和hashCode方法。
对于每一次操作(x, y), 如果(x, y)的上下左右都是0, 那么计数器加一; 如果不全为0, 则:
- 并查集查询其四周的1所属的集合, 假设它们属于 k 个不同的集合
- 计数器减去 k-1
- 将这 k 个集合, 连同 (x, y), 合并
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/ public class Solution {
/**
* @param n: An integer
* @param m: An integer
* @param operators: an array of point
* @return: an integer array
*/ class MyPoint{
int x;
int y;
MyPoint() { x = 0; y = 0; }
MyPoint(int a, int b) { x = a; y = b; } @Override
public boolean equals(Object obj) {
if (obj == this)
return true;
return this.x == ((MyPoint) obj).x && this.y == ((MyPoint) obj).y;
} @Override
public int hashCode() {
return x * 10 + y;
}
}
class UnionFind{
HashMap<MyPoint, MyPoint> father = new HashMap<>();
UnionFind(int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
MyPoint p = new MyPoint(i, j);
father.put(p, p);
}
}
} MyPoint find(MyPoint p) {
MyPoint point = p;
while (point != father.get(point)) {
point = father.get(point);
}
MyPoint root = point;
while (p != father.get(p)) {
MyPoint tmp = father.get(p);
father.put(tmp, root);
p = tmp;
}
return root;
} void union(MyPoint p1, MyPoint p2) {
MyPoint root1 = find(p1);
MyPoint root2 = find(p2);
if (root1 != root2) {
father.put(root1, root2);
}
}
}
public List<Integer> numIslands2(int n, int m, Point[] operators) {
List<Integer> ans = new ArrayList<>();
if (operators == null || operators.length == 0) {
return ans;
}
int[] dx = {0, -1, 0, 1};
int[] dy = {1, 0, -1, 0};
int[][] island = new int[n][m];
UnionFind uf = new UnionFind(n, m);
int count = 0;
for (int i = 0; i < operators.length; i++) {
int x = operators[i].x;
int y = operators[i].y;
if (island[x][y] != 1) {
island[x][y] = 1;
count++;
MyPoint p = new MyPoint(x, y);
for (int j = 0; j < 4; j++) {
int nx = x + dx[j];
int ny = y + dy[j];
if (0 <= nx && nx < n && 0 <= ny && ny < m && island[nx][ny] == 1) {
MyPoint np = new MyPoint(nx, ny);
MyPoint root1 = uf.find(p);
MyPoint root2 = uf.find(np);
if (root1 != root2) {
count--;
uf.union(p, np);
}
}
}
}
ans.add(count);
}
return ans;
}
}
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] 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] Number of Islands II
Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...
- 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 ...
- 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 ...
- 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 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 ...
- LintCode "Number of Islands II"
A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah ...
随机推荐
- 关于tomcat9的startup.bat闪退问题&乱码
1.打开apache-tomcat-9.0.22的bin目录 选择图中文件 记事本打开 2.更改文件 setclasspath.bat .0_172\jre 3.保存 startup ----- ...
- QT http请求数据
1.创建一个请求类(HttpWork): HttpWork.h头文件 #pragma once #include <QObject> #include <QNetworkAccess ...
- 2019秋季PAT甲级_备考总结
2019 秋季 PAT 甲级 备考总结 在 2019/9/8 的 PAT 甲级考试中拿到了满分,考试题目的C++题解记录在这里,此处对备考过程和考试情况做一个总结.如果我的方法能帮助到碰巧点进来的有缘 ...
- PHP get和post向服务器发送请求
1 .get请求 <?php //请求url地址 $token="xxx"; $url = "请求的地址"; //初始化curl $ch = curl_i ...
- PAT(B) 1087 有多少不同的值(Java)规律
题目链接:1087 有多少不同的值 (20 point(s)) 题目描述 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取 ...
- OpenCV学习笔记3
OpenCV学习笔记3 图像平滑(低通滤波) 使用低通滤波器可以达到图像模糊的目的.这对与去除噪音很有帮助.其实就是去除图像中的高频成分(比如:噪音,边界).所以边界也会被模糊一点.(当然,也有一些模 ...
- Scala 面向对象编程之Trait
将trait作为接口使用 // Scala中的Triat是一种特殊的概念 // 首先我们可以将Trait作为接口来使用,此时的Triat就与Java中的接口非常类似 // 在triat中可以定义抽象方 ...
- Luogu5307 [COCI2019] Mobitel 【数论分块】【递推】
题目分析: 对于向上取整我们总有,$\lceil \frac{\lceil \frac{n}{a} \rceil}{b} \rceil = \lceil \frac{n}{a*b} \rceil$这个 ...
- 在论坛中出现的比较难的sql问题:40(子查询 销售和历史库存)
原文:在论坛中出现的比较难的sql问题:40(子查询 销售和历史库存) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...
- 3_PHP表达式_5_数据类型转换_类型自动转换
以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. PHP类型转换分为类型自动转换和类型强制转换. 1.布尔型数据参与算数运算时,TRUE被转换为整数1,FALSE被 ...