

나의 풀이
class Solution {
public int[] solution(int brown, int yellow) {
int[] answer = new int[2];
int sum = brown + yellow;
for (int i = 3; i <= sum; i++) { // 1
int width = sum / i;
if (width >= i && (i - 2) * (width - 2) == yellow) { // 2
answer[0] = width;
answer[1] = i;
break;
}
}
return answer;
}
}
과정
- brown과 yellow의 합의 공약수를 구하기 위한 반복문을 선언(노란색이 가운데 들어가야 하므로 3이상 부터)
- 가로가 세로와 같거나 더 길고, 가운데 yellow가 들어갈 크기와 같다면 answer에 가로, 세로를 각각 넣어준다
다른 사람 풀이
import java.util.*;
class Solution {
public int[] solution(int brown, int red) {
int a = (brown+4)/2;
int b = red+2*a-4;
int[] answer = {(int)(a+Math.sqrt(a*a-4*b))/2,(int)(a-Math.sqrt(a*a-4*b))/2};
return answer;
}
}