solution(10, 2) // [4, 3]
solution(8, 1) // [3, 3]
solution(24, 24) // [8, 6]
function solution(brown, yellow) {
let answer = [];
for(width = 1; width <= yellow; width++) {
let height = yellow / width;
if (width >= height) {
if (brown === width * 2 + 2 + height * 2 + 2) {
answer = [width+2, height+2];
}
}
}
return answer;
}
문제를 이해하니 풀이는 쉬웠다. yellow를 가로로 나눴을 때 리턴값과, 노란색 카펫의 격자수(x, y) -> 갈색 테두리 (x2 + y2 + 4; 여기서 4=모서리값)의 리턴값이 같을경우 정답을 리턴하는 식으로 풀이했다.
출처: 프로그래머스