Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 35c30f2

Browse files
iHoHyeoncodeisneverodd
andauthored
solution level2 - 숫자 블록 (#79)
* solution level2 - 숫자 블록 * Update level-2/숫자-블록.js Co-authored-by: codeisneverodd <codeisneverodd@gmail.com> Co-authored-by: codeisneverodd <codeisneverodd@gmail.com>
1 parent d5a31a3 commit 35c30f2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎level-2/숫자-블록.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
//정답 1 - iHoHyeon
4+
function solution(begin, end) {
5+
return new Array(end - begin + 1).fill(null).map((v, idx) => calc(begin + idx));
6+
// begin ~ end 각 위치에 대해서 calc 함수의 return 값으로 채운다.
7+
}
8+
9+
const calc = (number) => {
10+
if (number === 1) return 0;
11+
// 1번째 위치는 무조건 0블록이 위치
12+
13+
for (let i = 2; i <= Math.sqrt(number); i++) {
14+
if (number % i === 0 && number / i <= 10_000_000) return number / i;
15+
// 10_000_000번 블록까지만 놓았으므로 숫자를 초과하는 경우는 제외
16+
}
17+
18+
return 1;
19+
};
20+
21+
/*
22+
1번 블록부터 10_000_000번 블록까지 전부 규칙에 따라서 놓는 경우는
23+
시간 / 공간 복잡도가 급상승
24+
25+
-> 따라서 각 위치에 어떤 숫자의 블록이 놓일지를 계산해주자
26+
27+
-> n번째 위치에는 1, n을 제외한 n의 가장 큰 약수의 블록이 놓이게 된다.
28+
29+
-> 가장 큰 약수는 n / (n의 가장 작은 약수)임을 이용해서 계산해주면 된다.
30+
31+
+ 가장 큰 약수가 1인 경우는 소수인 경우이고 숫자 1 블록이 놓인다.
32+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /