🫡 문제
프로그래머스 - 카펫(완전 탐색)
🫡 코드
class Solution {
public int[] solution(int brown, int yellow) {
int[] answer = new int[2];
int total = brown + yellow;
for(int i=1; i<=total; i++){
if(total % i == 0){
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;
}
}
🫡 풀이
- answer[0] * answer[1] =
brown + yellow
brown + yellow 의 약수만 answer의 경우의 수가 될 수 있다.
brown >= yellow
yellow가 brown의 가운데 와야 하기 때문에 brown 이 더 커야 한다.
(width-2)*(height-2) 결과가 yellow와 같을 때 yellow 격자 수와 동일하다.