-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
959.java
150 lines (120 loc) · 4.06 KB
/
959.java
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
__________________________________________________________________________________________________
sample 2 ms submission
class Solution {
public int regionsBySlashes(String[] grid) {
// F -1 = E + C - V
//Vertex: if connected component does not change ut add an edge
int[] u = new int[(grid.length+1)*(grid.length+1)];
for(int i=0; i<=grid.length; i++)
for(int j=0; j<=grid.length; j++){
u[i*grid.length + j] = i*grid.length + j;
}
//union
int count =1 ;
for(int i=0; i<grid.length;i++)
{
union(i, i+1, u);
union(i*grid.length, grid.length * (i+1),u);
union(grid.length*grid.length+i, grid.length*grid.length+i+1,u);
union(i*grid.length + grid.length, grid.length * (i*1) + grid.length,u);
}
for(int i=0; i< grid.length; i++){
char[] c = grid[i].toCharArray();
for(int j=0; j<c.length; j++){
int c_m=0; int c_n=0;
int n_m=0; int n_n=0;
if(c[j] == ' ') continue;
else if(c[j]=='/'){
c_m = (i+1)*grid.length;
c_n = j;
n_m = i*grid.length;
n_n = j+1;
}
else if(c[j]=='\\'){
c_m = i*grid.length;
c_n = j;
n_m = (i+1)*grid.length;
n_n = j+1;
}
if(connected(c_m+c_n,n_m+n_n,u)) count++;
union(c_m+c_n,n_m+n_n,u);
}
}
return count;
}
void union(int i, int j, int[] u){
int root_i = root(i, u);
int root_j = root(j, u);
u[root_i] = j;
}
int root(int i, int[] u){
while(i!=u[i]){
u[i] = u[u[i]];
i = u[i];
}
return i;
}
boolean connected(int i, int j, int[] u){
return root(i,u) == root(j,u);
}
}
__________________________________________________________________________________________________
sample 36884 kb submission
class Solution {
public int regionsBySlashes(String[] grid) {
int N = grid.length;
DSU dsu = new DSU(4 * N * N);
for (int r = 0; r < N; ++r)
for (int c = 0; c < N; ++c) {
int root = 4 * (r * N + c);
char val = grid[r].charAt(c);
if (val != '\\') {
dsu.union(root + 0, root + 1);
dsu.union(root + 2, root + 3);
}
if (val != '/') {
dsu.union(root + 0, root + 2);
dsu.union(root + 1, root + 3);
}
// north south
if (r + 1 < N)
dsu.union(root + 3, (root + 4 * N) + 0);
if (r - 1 >= 0)
dsu.union(root + 0, (root - 4 * N) + 3);
// east west
if (c + 1 < N)
dsu.union(root + 2, (root + 4) + 1);
if (c - 1 >= 0)
dsu.union(root + 1, (root - 4) + 2);
}
/**
north west east south 0 1 2 3
**/
return dsu.size();
}
}
class DSU {
int[] parent;
int size;
public DSU(int N) {
parent = new int[N];
size = N;
for (int i = 0; i < N; ++i)
parent[i] = i;
}
public int find(int x) {
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
public void union(int x, int y) {
if (find(x) == find(y)) {
return;
}
parent[find(x)] = find(y);
size--;
}
public int size() {
return size;
}
}
__________________________________________________________________________________________________