본문 바로가기
알고리즘/완전탐색

[프로그래머스 연습문제 level_2 ] 카펫

by happyhelen 2024. 1. 18.

 

class Solution {
    public int[] solution(int brown, int yellow) {
        int[] answer = new int[2];
        int cnt = 0;
        
        // yellow로 가로x세로 모든 조합을 찾는다
        int limit = (int)Math.sqrt(yellow);
        
        for(int i=1; i<= limit; i++){
            if(yellow % i == 0){
                // i가 작고 몫이 길다
                int wid = yellow / i; // 몫
                cnt = (i*2) + (wid*2) + 4; // 가로 칸 + 세로 칸 + 꼭짓점 칸
                
                if(cnt == brown){
                    answer[0] = wid+2;
                    answer[1] = i+2;
                    break;
                }
            }
        }
        
        return answer;
    }
}