LeetCode Number of Islands II
2016-02-21 09:05
295 查看
原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/
A 2d grid map of
Example:
Given
Initially, the 2d grid
Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land.
Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land.
Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land.
Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land.
We return the result as an array:
Challenge:
Can you do it in time complexity O(k log mn), where k is the length of the
Union-Find 题目。是Number of Islands升级版。
一个operation 添加进UnionFind2D型的islands. 看四个方向上islands里parent 对应的值,若是大于0, 就看是不是在一个跟下,若不在,就union起来,同时。四个direction走完,把islands的当前count加入res中。
Time Complexity: O(k*logmn). Space: O(mn).
AC Java:
A 2d grid map of
mrows and
ncolumns 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
gridis 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?
Union-Find 题目。是Number of Islands升级版。
一个operation 添加进UnionFind2D型的islands. 看四个方向上islands里parent 对应的值,若是大于0, 就看是不是在一个跟下,若不在,就union起来,同时。四个direction走完,把islands的当前count加入res中。
Time Complexity: O(k*logmn). Space: O(mn).
AC Java:
public class Solution { int [][] directions = {{0,1},{0,-1},{-1,0},{1,0}}; public List<Integer> numIslands2(int m, int n, int[][] positions) { List<Integer> res = new ArrayList<Integer>(); if(m == 0 || n == 0 || positions == null || positions.length == 0){ return res; } UnionFind2D islands = new UnionFind2D(m, n); for(int [] row : positions){ int p = islands.add(row[0],row[1]); for(int [] dir : directions){ int x = row[0] + dir[0]; int y = row[1] + dir[1]; int q = islands.getParent(x,y); if(q > 0 && !islands.find(p,q)){ islands.union(p,q); } } res.add(islands.size()); } return res; } } class UnionFind2D{ private int [] parent; private int [] size; private int count, m, n; public UnionFind2D(int m, int n){ this.m = m; this.n = n; this.count = 0; parent = new int[m*n+1]; size = new int[m*n+1]; } private int getIndex(int i, int j){ return i*n + j + 1; } public int add(int i, int j){ int index = getIndex(i,j); count++; parent[index] = index; size[index] = 1; return index; } public int getParent(int i, int j){ if(i < 0 || i>=m || j<0 || j>=n){ return 0; } return parent[getIndex(i,j)]; } public int size(){ return this.count; } public boolean find(int p, int q){ return root(p) == root(q); } private int root(int i){ if(parent[i] == 0){ return i; } while(i != parent[i]){ parent[i] = parent[parent[i]]; i = parent[i]; } return i; } public void union(int p, int q){ int i = root(p); int j = root(q); if(size[i] < size[j]){ parent[i] = j; size[j] += size[i]; }else{ parent[j] = i; size[i] += size[j]; } count--; } }
相关文章推荐
- 扫描文件夹获取包含子文件夹在内的所有文件
- String使用字面值常量赋值与new String()赋值的区别
- NEC 框架规范 css function
- LeetCode Roman to Integer
- jqMobi(App Framework)入门学习(一)
- codeforces 629D. Babaei and Birthday Cake
- bit-map牛刀小试:数组test[X]的值所有在区间[1, 8000]中, 现要输出test中反复的数。要求:1. 不能改变原数组; 2.时间复杂度为O(X);3.除test外空间不超过1KB
- 全面详细的jQuery常见开发技巧手册
- Hive Operators and User-Defined Functions (UDFs)
- php课程---建立一个简单的下拉列表框
- Android 开源项目android-open-project解析之(三) ScrollView,TimeView,TipView,FlipView
- php课程---初学练习
- 完善的jquery处理机制
- php课程---JavaScript改变HTML中的元素
- html5 isPointInPath相关操作
- 从头開始写项目Makefile(七):统一目标输出文件夹
- 使用工具来提升Android开发效率
- [LeetCode 329] Longest Increasing Path in a Matrix
- Android读取assets中的文件
- php课程---Windows.open()方法参数详解