-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpg_68936.java
More file actions
38 lines (32 loc) · 1.21 KB
/
pg_68936.java
File metadata and controls
38 lines (32 loc) · 1.21 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
class solutionWeek3C{
public int[] Solution(int[][] arr) {
return quadtree(arr, 0, 0, arr[0].length);
}
public int[] quadtree(int[][] arr, int x, int y, int len){
boolean isSame = true;
//x, y는 배열의 시작 인덱스
for(int i=x; i<x+len; i++){
for(int j=y; j<y+len; j++){
//압축이 안 되는 경우
if(arr[i][j] != arr[x][y]){
isSame=false;
break;
}
}
if(isSame==false) break;
}
//압축이 되는 경우
if(isSame==true){
if (arr[x][y]==0) return new int[]{1,0};
else return new int[]{0,1};
}
int halfLen = len/2;
int[] topLeft = quadtree(arr, x, y, halfLen);
int[] topLight = quadtree(arr, x + halfLen, y, halfLen);
int[] bottomLeft = quadtree(arr, x, y + halfLen, halfLen);
int[] bottomLight = quadtree(arr, x + halfLen, y + halfLen, halfLen);
return new int[] {
topLeft[0]+topLight[0]+bottomLeft[0]+bottomLight[0],
topLeft[1]+topLight[1]+bottomLeft[1]+bottomLight[1]};
}
}