Skip to content

solution level2 - 숫자 블록 #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions level-2/숫자-블록.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//https://github.com/codeisneverodd/programmers-coding-test
//완벽한 정답이 아닙니다.
//정답 1 - iHoHyeon
function solution(begin, end) {
return new Array(end - begin + 1).fill(null).map((v, idx) => calc(begin + idx));
// begin ~ end 각 위치에 대해서 calc 함수의 return 값으로 채운다.
}

const calc = (number) => {
if (number === 1) return 0;
// 1번째 위치는 무조건 0블록이 위치

for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0 && number / i <= 10_000_000) return number / i;
// 10_000_000번 블록까지만 놓았으므로 숫자를 초과하는 경우는 제외
}

return 1;
};

/*
1번 블록부터 10_000_000번 블록까지 전부 규칙에 따라서 놓는 경우는
시간 / 공간 복잡도가 급상승

-> 따라서 각 위치에 어떤 숫자의 블록이 놓일지를 계산해주자

-> n번째 위치에는 1, n을 제외한 n의 가장 큰 약수의 블록이 놓이게 된다.

-> 가장 큰 약수는 n / (n의 가장 작은 약수)임을 이용해서 계산해주면 된다.

+ 가장 큰 약수가 1인 경우는 소수인 경우이고 숫자 1 블록이 놓인다.
*/