Skip to content
Open
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions raejun/다음큰숫자.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function solution(n) {
var answer = 0;

const findOneCount = (number) => {
return number
.toString(2)
.split("")
.filter((n) => n === "1").length;
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수로 따로 뺀 것도 좋은 거 같아요!


const nCount = findOneCount(n);

let findNum = n + 1;

while (42) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

42 무릎탁 칩니다

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

센스 👍

if (findOneCount(findNum) === nCount) return findNum;

findNum++;
}
}

/*
풀이 시간: 31분

시간 복잡도는 O(n log n)이다.
공간 복잡도는 O(1)이다.

n의 1의 개수를 구하는 findOneCount 함수를 정의했다.
그 다음에, n보다 큰 수를 하나씩 증가시키면서, findOneCount 함수로 1의 개수를 구해서, n의 1의 개수와 비교했다.
만약 두 개수가 같으면, 그 수를 반환했다.

처음에는 n의 이진수를 구해서 이진수를 가지고 구해보려고 했는데, 깊은 생각이 요구될 것 같아 그냥 하나씩 증가시키면서 구하는 방법으로 풀이했다.
*/
38 changes: 38 additions & 0 deletions raejun/숫자의표현.js
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이렇게 접근하는 방법은 생각 못해봤는데, 한 수 배웠습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function solution(n) {
var answer = 1;

if (n === 1) return 1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

얼리 리턴 좋네요 👍


for (let i = Math.ceil(n / 2); i >= 0; i--) {
let sum = 0;

for (let j = i; j >= 0; j--) {
sum += j;

if (sum > n) {
break;
} else if (sum === n) {
answer++;
break;
}
}
}

return answer;
}

/*
풀이 시간: 16분

시간 복잡도는 O(n^2)이다.
공간 복잡도는 O(1)이다.

n이 1인 경우에는 1을 반환했다.
그 다음에, n의 절반부터 0까지 루프를 돌면서, i부터 0까지 루프를 돌면서, sum에 j를 더했다.
만약 sum이 n보다 크면, 내부 루프를 종료했다.
만약 sum이 n과 같으면, answer를 1 증가시키고, 내부 루프를 종료했다.
루프가 끝난 후에는 answer를 반환했다.

천장에서 아래로 내려오면서 더하는 방식으로 풀이했는데 n이 1인 경우 예외 처리를 찾지 못해서 오래 걸렸다.
DP로 풀이하는 방법도 있을 것 같고, for문은 한 번만 도는 방법도 있을 것 같은데 한 번 찾아봐야겠다.
*/
Loading