231007 쿼드압축 후 개수 세기

Jongleee·2023년 10월 7일
0

TIL

목록 보기
384/737
private int[] answer;

public int[] solution(int[][] arr) {
	answer = new int[2];
	countQuads(arr, 0, 0, arr.length);
	return answer;
}

private void countQuads(int[][] arr, int x, int y, int size) {
	int val = arr[x][y];

	if (isUniform(arr, x, y, size, val)) {
		answer[val]++;
		return;
	}

	int halfSize = size / 2;
	countQuads(arr, x, y, halfSize);
	countQuads(arr, x, y + halfSize, halfSize);
	countQuads(arr, x + halfSize, y, halfSize);
	countQuads(arr, x + halfSize, y + halfSize, halfSize);
}

private boolean isUniform(int[][] arr, int x, int y, int size, int val) {
	for (int i = x; i < x + size; i++) {
		for (int j = y; j < y + size; j++) {
			if (arr[i][j] != val) {
				return false;
			}
		}
	}
	return true;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/68936

0개의 댓글