본문 바로가기
코딩테스트 문제집/Programmers(Lv2)

[Programmers] Lv2: 카펫(42842)

by cogito30 2025. 2. 12.
반응형

문제

- 링크: https://school.programmers.co.kr/learn/courses/30/lessons/42842?language=java

 

풀이

더보기
class Solution {
    public int[] solution(int brown, int yellow) {
        int[] answer = new int[]{0, 0};
        int total = brown + yellow;
        for (int width = 1; width < total; width++) {
            if (total % width != 0) {
                continue;
            }
            
            int height = total / width;
            
            if (2 * (height + width - 2) == brown && (height - 2) * (width - 2) == yellow) {
                answer[0] = height;
                answer[1] = width;
                break;
            }
            
        }
        return answer;
    }
}

 

반응형