본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] 카펫

by 현장 2025. 11. 18.

-Code

class Solution {
    public int[] solution(int brown, int yellow) {
        int total = brown + yellow;

        for (int h = 3; h <= Math.sqrt(total); h++) {
           if (total % h == 0) {
                int w = total / h;
                if ((w - 2) * (h - 2) == yellow) {
                    return new int[]{w, h};
                }
            }
        }

        return new int[] {0};
    }
}

yellow의 경우도 확인해야 하는데 해당 부분을 처음에 빠뜨려서 그걸 고치고 해결했습니다.