본문 바로가기
카테고리 없음

[Java] 프로그래머스 - 쿼드압축 후 개수 세기

by C0MPAS 2024. 1. 19.

1월 19일(금) - 5장 재귀

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


public class Solution {

    private static class Count{
        public int zero;
        public int one;

        public Count(int zero, int one){
            this.zero = zero;
            this.one = one;
        }

        public Count add(Count other){
            return new Count(zero + other.zero, one + other.one);
        }
    }

    private Count count(int offset_x, int offset_y, int size, int[][] arr){
        int h = size/2;
        for(int x = offset_x; x<offset_x + size; x++)
        {
            for(int y = offset_y; y<offset_y + size; y++)
            {
                if(arr[y][x] != arr[offset_y][offset_x])
                {
                    return count(offset_x, offset_y, h, arr)
                            .add(count(offset_x+h, offset_y, h, arr))
                            .add(count(offset_x, offset_y+h, h, arr))
                            .add(count(offset_x+h, offset_y+h, h, arr));
                }
            }
        }

        if(arr[offset_y][offset_x] == 1)
        {
            return new Count(0,1);
        }
        return new Count(1,0);
    }

    public int[] solution(int[][] arr){
        Count count = count(0,0,arr.length, arr);
        return new int[] {count.zero, count.one};
    }
}

댓글