-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsland.java
More file actions
128 lines (103 loc) · 2.46 KB
/
Island.java
File metadata and controls
128 lines (103 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.util.*;
public class Island{
public static class Node{
private Integer value;
public Node(int c){
value = c;
}
}
private HashMap<Node,Node> parents;
private HashMap<Integer,Node> nodes;
private HashMap<Node,Integer> sizeMap;
public Island(int[][] arr){
parents = new HashMap<Node,Node>();
nodes = new HashMap<Integer,Node>();
sizeMap = new HashMap<Node,Integer>();
for (int i=0; i<arr.length;i++ ) {
for (int j=0; j<arr[i].length; j++) {
if(arr[i][j]==1){
Node node = new Node(arr[i][j]);
parents.put(node,node);
nodes.put(i*arr.length+j,node);
sizeMap.put(node,1);
}
}
}
for (int i=0; i<arr[0].length-1; i++) {
if(arr[0][i]==1 && arr[0][i+1]==1){
Union(i,i+1);
//System.out.println("top1");
}
}
for (int i=1; i<arr.length-1; i++) {
if(arr[i][0]==1 && arr[i-1][0]==1){
int index = i* arr.length;
int topIndex = (i-1)* arr.length;
Union(index,topIndex);
//System.out.println("left1");
}
}
for (int i=1; i<arr.length;i++ ) {
for (int j=1; j<arr[i].length; j++) {
if(arr[i][j]==1 && arr[i-1][j]==1){
int index = i* arr.length+j;
int topIndex = (i-1)* arr.length+j;
Union(index,topIndex);
//System.out.println("top2");
}
if(arr[i][j]==1 && arr[i][j-1]==1){
int index = i* arr.length+j;
Union(index,index-1);
//System.out.println("left2");
}
}
}
}
private Node Find(Integer node){
Stack<Node> stack = new Stack<Node>();
Node cur = nodes.get(node);
while(cur != parents.get(cur)){
stack.push(cur);
cur = parents.get(cur);
}
while(!stack.isEmpty()){
parents.put(stack.pop(),cur);
}
return cur;
}
public void Union(Integer a,Integer b){
Node fa = Find(a);
Node fb = Find(b);
if(fa != fb){
int sa = sizeMap.get(fa);
int sb = sizeMap.get(fb);
Node big = sa>=sb ? fa : fb;
Node small = big==fa ? fb : fa;
parents.put(small,big);
sizeMap.put(big,sizeMap.get(big)+sizeMap.get(small));
sizeMap.remove(small);
}
}
public int size(){
return sizeMap.size();
}
public int getMax(){
int max=-1;
for(Integer key:sizeMap.values()){
max = Math.max(key,max);
}
return max;
}
public static void main(String[] args){
int[][] matrix={
{1,1,0,0,1},
{1,0,1,0,1},
{0,1,1,0,1},
{0,0,1,1,1},
{1,0,0,1,1}
};
Island unionFind = new Island(matrix);
System.out.println("size ="+ unionFind.size());
System.out.println("max ="+ unionFind.getMax());
}
}