https://programmers.co.kr/learn/courses/30/lessons/42842?language=javascript
노란색의 개수를 찾고 brown과 맞춰보는 방법
//yellow의 약수를 쌍으로 찾기
function yellowDivisor(yellow, i) {
if(yellow % i === 0){
return [yellow/i, i];
}
}
function solution(brown, yellow) {
var answer = [];
for (let i = 0; i <= yellow; i++) {
//i가 yellow를 나눌 수 없는 숫자일 경우 undefined가 반환되니까 넘겨주기
if(yellowDivisor(yellow, i) === undefined) continue;
//약수의 합 * 2 + 4 하면 노란색을 두르고 있는 갈색의 개수
let brownCount = yellowDivisor(yellow, i).reduce((prev, curr) => prev + curr, 0) * 2 + 4;
//갈색의 수가 주어진 brown과 같다면 [노란색 가로 + 2, 노란색 세로 + 2]가 카펫의 크기
if (brownCount === brown) {
answer = yellowDivisor(yellow, i).map((el) => el + 2).sort((a,b) => b - a);
}
}
return answer;
}
갈색의 개수를 찾고 yellow와 맞춰보는 방법
입출력 예를 보면 brown + yellow = (return[0] * return[1])
인 것을 알 수 있다.
function solution(brown, yellow) {
var answer = [];
const sum = brown + yellow;
for (let height = 3; height <= brown; height++) {
if (sum % height === 0) {
let width = sum / height;
if ((height - 2) * (width - 2) === yellow) {
return answer = [width, height];
}
}
}
}