Skip to content

[WEEK11] 이지현#51

Open
sik9252 wants to merge 2 commits into
mainfrom
sik9252
Open

[WEEK11] 이지현#51
sik9252 wants to merge 2 commits into
mainfrom
sik9252

Conversation

@sik9252
Copy link
Copy Markdown
Collaborator

@sik9252 sik9252 commented Apr 22, 2026

이렇게 풀었어요

1. 숫자의 표현

  • 문제를 풀었어요.
  • 풀이 시간 : 15분

1) 복잡도 계산

시간 복잡도: O(n)

공간 복잡도: O(1)


2) 접근 아이디어

이 문제는 자연수 n연속된 자연수들의 합으로 나타내는 경우의 수를 구하는 문제이다.
연속된 자연수의 합은 하나의 구간 합으로 볼 수 있기 때문에, leftright 두 포인터를 이용해 현재 구간의 합을 관리하는 방식으로 해결할 수 있다.

현재 구간의 합 sumn과 같으면 경우의 수를 1 증가시키고, 왼쪽 값을 제외해 다음 구간을 탐색한다.
sumn보다 작으면 합이 부족하므로 오른쪽 포인터를 늘려 구간을 확장하고, sumn보다 크면 왼쪽 포인터를 이동시켜 구간을 줄인다.

모든 수가 자연수이기 때문에 포인터를 한 방향으로만 이동해도 되고, 각 포인터는 최대 n번 정도만 움직이므로 전체 시간 복잡도는 O(n)이다.


2. 다음 큰 숫자

  • 문제를 풀었어요.
  • 풀이 시간 : 10분

1) 복잡도 계산

시간 복잡도: O(k log n)

공간 복잡도: O(log n)


2) 접근 아이디어

이 문제는 n보다 큰 자연수 중에서, 2진수로 변환했을 때 1의 개수가 같고, 그중 가장 작은 수를 찾는 문제이다.

먼저 현재 숫자 n을 2진수 문자열로 바꿔 1의 개수를 구한다.
그다음 n + 1부터 차례대로 증가시키면서 각 숫자를 2진수로 변환하고, 1의 개수가 같은지 확인한다.
처음으로 조건을 만족하는 숫자가 나오면, 그 수가 n보다 크면서 가장 가까운 수이므로 바로 반환하면 된다.

문제에서 요구하는 값이 “조건을 만족하는 수 중 가장 작은 수”이기 때문에, 순차적으로 탐색하는 방식이 가장 직관적이고 구현도 간단하다.

Copy link
Copy Markdown
Collaborator

@raejun92 raejun92 left a comment

Choose a reason for hiding this comment

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

고생하셨습니다. 한 수 배워가요~

@@ -0,0 +1,12 @@
function solution(n) {
const countOne = (num) => num.toString(2).split("1").length - 1;
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.

-1은 없어도 되는 부분인 것 같아 보여요!

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.

와 이해하기 쉽지 않군요ㅠ
전혀 생각 못했는데 이런 생각을 어떻게 하는지 대단해요!

Copy link
Copy Markdown
Collaborator

@LeeBaeJin LeeBaeJin left a comment

Choose a reason for hiding this comment

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

수고많으셨습니다!

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.

Charlie Puth - Left Right Left 가 생각나네요

Copy link
Copy Markdown
Member

@doitchuu doitchuu left a comment

Choose a reason for hiding this comment

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

고생하셨습니다 👍

@@ -0,0 +1,12 @@
function solution(n) {
const countOne = (num) => num.toString(2).split("1").length - 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.

깔끔하네요! 👍

}

return answer;
}
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.

지현님 풀이 참고해서 공부해봐야겠네요 !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants