-
Notifications
You must be signed in to change notification settings - Fork 1
[WEEK11] 최준호 #48
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
base: main
Are you sure you want to change the base?
[WEEK11] 최준호 #48
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| }; | ||
|
|
||
| const nCount = findOneCount(n); | ||
|
|
||
| let findNum = n + 1; | ||
|
|
||
| while (42) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 42 무릎탁 칩니다
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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의 이진수를 구해서 이진수를 가지고 구해보려고 했는데, 깊은 생각이 요구될 것 같아 그냥 하나씩 증가시키면서 구하는 방법으로 풀이했다. | ||
| */ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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문은 한 번만 도는 방법도 있을 것 같은데 한 번 찾아봐야겠다. | ||
| */ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
함수로 따로 뺀 것도 좋은 거 같아요!