[프로그래머스] 카펫

JOY·2023년 6월 18일
0

[CodingTest] Java

목록 보기
38/61
post-thumbnail

🫡 문제

프로그래머스 - 카펫(완전 탐색)

🫡 코드

class Solution {
    public int[] solution(int brown, int yellow) {
        int[] answer = new int[2]; //brown 수, yellow수
        int total = brown + yellow;
        
        for(int i=1; i<=total; i++){
            if(total % i == 0){ //i가 total의 약수일 때
                int width = i;
                int height = total / i;
                
                if(width >= height){
                  int count = (width-2)*(height-2);
                    if(count == yellow){
                        answer[0] = width;
                        answer[1] = height;
                        break;
                    }
                }
            }
            
        }
        
        return answer;
    }
}

🫡 풀이

  1. answer[0] * answer[1] = brown + yellow
  • brown + yellow 의 약수만 answer의 경우의 수가 될 수 있다.
  1. brown >= yellow
  • yellowbrown의 가운데 와야 하기 때문에 brown 이 더 커야 한다.
  • (width-2)*(height-2) 결과가 yellow와 같을 때 yellow 격자 수와 동일하다.
profile
Just Do IT ------- 🏃‍♀️

0개의 댓글

관련 채용 정보