Skip to content

[WEEK07-1] 이지현#29

Merged
sik9252 merged 2 commits into
mainfrom
sik9252
Mar 25, 2026
Merged

[WEEK07-1] 이지현#29
sik9252 merged 2 commits into
mainfrom
sik9252

Conversation

@sik9252
Copy link
Copy Markdown
Collaborator

@sik9252 sik9252 commented Mar 22, 2026

이렇게 풀었어요

1. Backspace String Compare

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

1) 복잡도 계산

시간 복잡도: O(n + m)

공간 복잡도: O(n + m)


2) 접근 아이디어

이 문제는 문자열에서 #를 backspace처럼 처리했을 때, 최종적으로 두 문자열이 같은지를 비교하는 문제이다 Backspace, 즉 이전 문자를 지우는 형태는 풀이에 스택을 주로 사용했던것이 떠올랐다.

그래서 s, t 각각에 대해 별도의 calc 함수를 만들어 최종 문자열을 계산했다.

문자를 순회하면서 일반 문자는 스택에 넣고, #를 만나면 가장 최근에 들어간 문자를 제거하도록 구현했다. 만약 스택이 비어 있는데 #가 나오면 무시하도록 처리했다. 이렇게 각각의 문자열을 최종 형태로 만든 뒤, 두 결과 문자열이 같은지 비교하는 방식으로 문제를 풀었다.


3) 회고

이 문제를 통해 backspace 동작이 결국 가장 최근 입력을 취소하는 것이기 때문에 스택과 잘 맞는다는 점을 다시 느꼈다. 구현 자체는 어렵지 않았지만, #가 연속으로 나오거나 처음부터 #가 나오는 경우처럼 예외 상황을 자연스럽게 처리하는 것이 중요하다고 느꼈다.


2. Counting Bits

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

1) 복잡도 계산

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

공간 복잡도: O(n)


2) 접근 아이디어

이 문제는 0부터 n까지의 모든 정수를 이진수로 바꿨을 때, 각각 1이 몇 개 들어있는지를 배열로 반환하는 문제이다. 처음에는 각 숫자를 이진수 문자열로 변환한 뒤 1의 개수를 세는 방법도 떠올릴 수 있었지만, 문자열 변환 없이 비트 연산으로 처리하는 쪽이 더 적절하다고 생각했다.

이번에는 num = num & (num - 1) 연산을 활용해서 풀었다. 이 연산은 어떤 숫자에서 가장 오른쪽에 있는 1비트 하나를 제거하는 특징이 있다. 따라서 어떤 수에 대해 이 연산을 반복하면, 반복한 횟수가 곧 그 수에 포함된 1의 개수가 된다. 그래서 0부터 n까지 순회하면서 각 숫자마다 이 연산을 반복 적용하고, 카운트한 값을 배열에 넣어 결과를 만들었다.


3) 회고

비트 연산 관련 문제를 풀어본 적이 없었다면 오래 걸렸을 것 같은 문제였다.
또한 내가 풀이한 방식은 각 숫자마다 반복적으로 계산해야 한다. 풀이를 확인해보니 DP를 이용해 이전 결과를 재활용하는 더 효율적인 풀이가 있었는데 이것을 함께 익혀두면 좋겠다고 느꼈다.

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.

LGTM~

}
}

return [...stack].join("");
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.

stack이 배열인데 다시 배열에 넣어주고 있는 것 같아 보여요!

Comment thread sik9252/CountingBits.js
let count = 0;

while (num > 0) {
num = num & (num - 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.

여기 부분이 어떻게 동작하는지 설명 부탁드려도 될까요?!

Comment thread sik9252/CountingBits.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.

비트연산 지식이 장착되어 있으시다니 멋져요!

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.

항상 풀이가 깔끔한 거 같아요 👍
한 주도 고생하셨습니다~!

Comment thread sik9252/CountingBits.js
while (num > 0) {
num = num & (num - 1);
count++;
}
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.

오 이렇게 푸는방법도 있네요!!!

@sik9252 sik9252 merged commit b8b6aaa into main Mar 25, 2026
1 check passed
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.

3 participants