카펫


프로그래머스 [카펫]

문제보기
Alt text

소스코드

class Solution {
  public static int[] solution(int brown, int yellow) {

        int totalGrid = brown + yellow;

        for (int i = 3; i <= totalGrid; i++) {
            if (totalGrid % i != 0) continue;

            int x = i;
            int y = totalGrid / i;

            int brownCnt = (x * 2) + (y * 2) - 4;
            int yellowCnt = (x * y) - brownCnt;

            if (brownCnt == brown && yellowCnt == yellow) {
                return new int[]{Math.max(x, y), Math.min(x, y)};
            }
        }

        return null;
    }
}