LeetCode Number of Islands II

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode Number of Islands II相关的知识,希望对你有一定的参考价值。

原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/

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]

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:

 1 public class Solution {
 2     int [][] directions = {{0,1},{0,-1},{-1,0},{1,0}};
 3     public List<Integer> numIslands2(int m, int n, int[][] positions) {
 4         List<Integer> res = new ArrayList<Integer>();
 5         if(m == 0 || n == 0 || positions == null || positions.length == 0){
 6             return res;
 7         }
 8         
 9         UnionFind2D islands = new UnionFind2D(m, n);
10         for(int [] row : positions){
11             int p = islands.add(row[0],row[1]);
12             for(int [] dir : directions){
13                 int x = row[0] + dir[0];
14                 int y = row[1] + dir[1];
15                 int q = islands.getParent(x,y);
16                 if(q > 0 && !islands.find(p,q)){
17                     islands.union(p,q);
18                 }
19             }
20             res.add(islands.size());
21         }
22         return res;
23     }
24 }
25 
26 class UnionFind2D{
27     private int [] parent;
28     private int [] size;
29     private int count, m, n;
30     
31     public UnionFind2D(int m, int n){
32         this.m = m;
33         this.n = n;
34         this.count = 0;
35         parent = new int[m*n+1];
36         size = new int[m*n+1];
37     }
38     
39     private int getIndex(int i, int j){
40         return i*n + j + 1; 
41     }
42     
43     public int add(int i, int j){
44         int index = getIndex(i,j);
45         count++;
46         parent[index] = index;
47         size[index] = 1;
48         return index;
49     }
50     
51     public int getParent(int i, int j){
52         if(i < 0 || i>=m || j<0 || j>=n){
53             return 0;
54         }
55         return parent[getIndex(i,j)];
56     }
57     
58     public int size(){
59         return this.count;
60     }
61     
62     public boolean find(int p, int q){
63         return root(p) == root(q);
64     }
65     
66     private int root(int i){
67         if(parent[i] == 0){
68             return i;
69         }
70         while(i != parent[i]){
71             parent[i] = parent[parent[i]];
72             i = parent[i];
73         }
74         return i;
75     }
76     
77     public void union(int p, int q){
78         int i = root(p);
79         int j = root(q);
80         if(size[i] < size[j]){
81             parent[i] = j;
82             size[j] += size[i];
83         }else{
84             parent[j] = i;
85             size[i] += size[j];
86         }
87         count--;
88     }
89 }

 

以上是关于LeetCode Number of Islands II的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] Number of Distinct Islands 不同岛屿的个数

200. Number of Islands + 695. Max Area of Island

[LeetCode] Number of Distinct Islands II 不同岛屿的个数之二

[leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

LeetCode Max Area of Island

Max Area of Island——leetcode