Conversation
raejun92
approved these changes
Apr 22, 2026
| @@ -0,0 +1,12 @@ | |||
| function solution(n) { | |||
| const countOne = (num) => num.toString(2).split("1").length - 1; | |||
Collaborator
There was a problem hiding this comment.
-1은 없어도 되는 부분인 것 같아 보여요!
Collaborator
There was a problem hiding this comment.
와 이해하기 쉽지 않군요ㅠ
전혀 생각 못했는데 이런 생각을 어떻게 하는지 대단해요!
LeeBaeJin
approved these changes
Apr 23, 2026
Collaborator
There was a problem hiding this comment.
Charlie Puth - Left Right Left 가 생각나네요
doitchuu
approved these changes
Apr 23, 2026
| @@ -0,0 +1,12 @@ | |||
| function solution(n) { | |||
| const countOne = (num) => num.toString(2).split("1").length - 1; | |||
| } | ||
|
|
||
| return answer; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
이렇게 풀었어요
1. 숫자의 표현
1) 복잡도 계산
시간 복잡도: O(n)
공간 복잡도: O(1)
2) 접근 아이디어
이 문제는 자연수
n을 연속된 자연수들의 합으로 나타내는 경우의 수를 구하는 문제이다.연속된 자연수의 합은 하나의 구간 합으로 볼 수 있기 때문에,
left와right두 포인터를 이용해 현재 구간의 합을 관리하는 방식으로 해결할 수 있다.현재 구간의 합
sum이n과 같으면 경우의 수를 1 증가시키고, 왼쪽 값을 제외해 다음 구간을 탐색한다.sum이n보다 작으면 합이 부족하므로 오른쪽 포인터를 늘려 구간을 확장하고,sum이n보다 크면 왼쪽 포인터를 이동시켜 구간을 줄인다.모든 수가 자연수이기 때문에 포인터를 한 방향으로만 이동해도 되고, 각 포인터는 최대
n번 정도만 움직이므로 전체 시간 복잡도는O(n)이다.2. 다음 큰 숫자
1) 복잡도 계산
시간 복잡도: O(k log n)
공간 복잡도: O(log n)
2) 접근 아이디어
이 문제는
n보다 큰 자연수 중에서, 2진수로 변환했을 때1의 개수가 같고, 그중 가장 작은 수를 찾는 문제이다.먼저 현재 숫자
n을 2진수 문자열로 바꿔1의 개수를 구한다.그다음
n + 1부터 차례대로 증가시키면서 각 숫자를 2진수로 변환하고,1의 개수가 같은지 확인한다.처음으로 조건을 만족하는 숫자가 나오면, 그 수가
n보다 크면서 가장 가까운 수이므로 바로 반환하면 된다.문제에서 요구하는 값이 “조건을 만족하는 수 중 가장 작은 수”이기 때문에, 순차적으로 탐색하는 방식이 가장 직관적이고 구현도 간단하다.