[WEEK07-1] 추슬기#28
Merged
Merged
Conversation
raejun92
previously approved these changes
Mar 22, 2026
| * @return {number[]} | ||
| */ | ||
| var countBits = function(n) { | ||
| const dp = [0]; |
Collaborator
There was a problem hiding this comment.
dp로 구현하진 못했지만 dp를 써야한다는 건 알았던 걸까요?!
sik9252
previously approved these changes
Mar 25, 2026
Collaborator
sik9252
left a comment
There was a problem hiding this comment.
둘 다 로직이 짧고 간단해서 직관적으로 읽힌다는 강점이 있는 것 같아요, Counting Bits 푸는 다른 방법도 알아갑니다
raejun92
approved these changes
Mar 26, 2026
sik9252
approved these changes
Mar 30, 2026
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. Backspace String Compare
1) 복잡도 계산
2) 접근 아이디어
#개수를 세고 남는 문자를 비교하려고 했다.#를 만나면 pop, 문자를 만나면 push해서 최종 문자열을 비교했다.3) 회고
첫 풀이에서는 문자열을 사람이 생각한 순서 그대로 처리하려고 하다 보니 예외 케이스 대응이 잘 안 됐다.
뒤에서부터 보면서 직접 지우는 흐름으로 풀 수 있을 것 같았는데, 실제로 구현하니 인덱스 처리랑 backspace 개수 관리가 계속 꼬였다.
처음에는 이런 식으로 풀어보려고 했다.
스택으로 바꾸고 나니 훨씬 단순해졌다.
문자를 순회하면서
#면 제거하고, 아니면 넣는 방식이라 문제 구조가 더 명확하게 보였다.2. Counting Bits
1) 복잡도 계산
2) 접근 아이디어
0부터n까지 순회하면서 각 숫자를 이진 문자열로 바꿨다.0을 제거한 뒤 남은 길이를 1의 개수로 계산했다.dp배열에 차례대로 넣어 결과 배열을 만들었다.3) 회고
이번 풀이는 문자열 변환으로 바로 개수를 세는 방식이라 구현은 쉬웠다.
다만 다른 사람 풀이를 보니, 이 문제는 진짜 DP답게 “이미 계산한 값을 재사용”하는 방식으로 더 깔끔하게 풀 수 있다는 걸 알게 됐다.
다른 사람 풀이:
다른 사람 풀이 핵심:
i >> 1은 이미 계산한 절반 값이고,(i & 1)은 마지막 비트가 1인지 여부라서 이전 결과를 그대로 재사용할 수 있었다.내 생각: 문자열 변환 없이
dp[i >> 1]를 활용해 비트 개수를 계산하는 방식이 훨씬 DP답게 느껴졌다. DP는 결국 “이미 계산한 값 재사용”이라는 점을 다시 생각하면서 한 번 더 풀어보면 좋을 것 같다.