Skip to content

Commit 1578c8c

Browse files
committed
pg_68936
1 parent e452bf2 commit 1578c8c

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/week03/Chanjong/pg_68936.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public int[] solution(int[][] arr) {
3+
return quadtree(arr, 0, 0, arr[0].length);
4+
}
5+
6+
public int[] quadtree(int[][] arr, int x, int y, int len){
7+
boolean isSame = true;
8+
9+
//x, y는 배열의 시작 인덱스
10+
for(int i=x; i<x+len; i++){
11+
for(int j=y; j<y+len; j++){
12+
//압축이 안 되는 경우
13+
if(arr[i][j] != arr[x][y]){
14+
isSame=false;
15+
break;
16+
}
17+
}
18+
if(isSame==false) break;
19+
}
20+
21+
//압축이 되는 경우
22+
if(isSame==true){
23+
if (arr[x][y]==0) return new int[]{1,0};
24+
else return new int[]{0,1};
25+
}
26+
27+
int halfLen = len/2;
28+
29+
int[] topLeft = quadtree(arr, x, y, halfLen);
30+
int[] topLight = quadtree(arr, x + halfLen, y, halfLen);
31+
int[] bottomLeft = quadtree(arr, x, y + halfLen, halfLen);
32+
int[] bottomLight = quadtree(arr, x + halfLen, y + halfLen, halfLen);
33+
34+
return new int[] {
35+
topLeft[0]+topLight[0]+bottomLeft[0]+bottomLight[0],
36+
topLeft[1]+topLight[1]+bottomLeft[1]+bottomLight[1]};
37+
}
38+
}

0 commit comments

Comments
 (0)