Skip to content

[WEEK07-1] 추슬기#28

Merged
doitchuu merged 4 commits into
pnt-fe-study:mainfrom
doitchuu:doitchuu
Mar 30, 2026
Merged

[WEEK07-1] 추슬기#28
doitchuu merged 4 commits into
pnt-fe-study:mainfrom
doitchuu:doitchuu

Conversation

@doitchuu
Copy link
Copy Markdown
Member

이렇게 풀었어요

1. Backspace String Compare

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

1) 복잡도 계산

  • 시간 복잡도: O(n)
  • 공간 복잡도: O(n)

2) 접근 아이디어

  1. 처음에는 문자열을 뒤에서부터 직접 확인하면서 # 개수를 세고 남는 문자를 비교하려고 했다.
  2. 예외 케이스가 많아져서 문자열을 직접 조작하는 방식 대신 스택으로 바꿨다.
  3. 각 문자열에서 #를 만나면 pop, 문자를 만나면 push해서 최종 문자열을 비교했다.

3) 회고

첫 풀이에서는 문자열을 사람이 생각한 순서 그대로 처리하려고 하다 보니 예외 케이스 대응이 잘 안 됐다.
뒤에서부터 보면서 직접 지우는 흐름으로 풀 수 있을 것 같았는데, 실제로 구현하니 인덱스 처리랑 backspace 개수 관리가 계속 꼬였다.

처음에는 이런 식으로 풀어보려고 했다.

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var backspaceCompare = function(s, t) {
    // 1. 투포인터로 두고 각 문자열을 확인한다. 
    // 2. 만약 #을 만나면, 해당 방식을 지운다. 
    const maxLength = Math.max(s.length, t.length) - 1;
    let sWord = "";
    let tWord = "";
    let sBackspace = 0;
    let tBackspace = 0;

    // s 문자를 뒤로 부터 확인하면서 #이 있으면 앞의 문자를 찾으면 해당 문자를 지운다. 
    // 전체 문자를 순회해서 새로운 문자열 s2와 t2를 비교한다.

    for (let i = maxLength; i <= 0; i--) {
        if (sBackspace && s[i] !== "#") {
            sWord += s[i];
            sBackspace--;
        }

        if (tBackspace && t[i] !== "#") {
            tWord += t[i];
            tBackspace--;
        }

        if (s[i] && s[i] !== "#") {
            sWord += s[i];
        } else {
            sBackspace++;
        }

        if (t[i] && t[i] !== "#") {
            tWord += t[i];
        } else {
            tBackspace++;
        }
    }

    console.log(sWord, tWord);

    return sWord === tWord;
};

스택으로 바꾸고 나니 훨씬 단순해졌다.
문자를 순회하면서 #면 제거하고, 아니면 넣는 방식이라 문제 구조가 더 명확하게 보였다.



2. Counting Bits

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

1) 복잡도 계산

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

2) 접근 아이디어

  1. 0부터 n까지 순회하면서 각 숫자를 이진 문자열로 바꿨다.
  2. 변환된 문자열에서 0을 제거한 뒤 남은 길이를 1의 개수로 계산했다.
  3. 계산한 값을 dp 배열에 차례대로 넣어 결과 배열을 만들었다.

3) 회고

이번 풀이는 문자열 변환으로 바로 개수를 세는 방식이라 구현은 쉬웠다.
다만 다른 사람 풀이를 보니, 이 문제는 진짜 DP답게 “이미 계산한 값을 재사용”하는 방식으로 더 깔끔하게 풀 수 있다는 걸 알게 됐다.

다른 사람 풀이:

var countBits = function(n) {
    const dp = new Array(n + 1).fill(0);

    for (let i = 1; i <= n; i++) {
        dp[i] = dp[i >> 1] + (i & 1);
    }

    return dp;
};

다른 사람 풀이 핵심: i >> 1은 이미 계산한 절반 값이고, (i & 1)은 마지막 비트가 1인지 여부라서 이전 결과를 그대로 재사용할 수 있었다.
내 생각: 문자열 변환 없이 dp[i >> 1]를 활용해 비트 개수를 계산하는 방식이 훨씬 DP답게 느껴졌다. DP는 결국 “이미 계산한 값 재사용”이라는 점을 다시 생각하면서 한 번 더 풀어보면 좋을 것 같다.



raejun92
raejun92 previously approved these changes Mar 22, 2026
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!

Comment thread doitchuu/CountingBits.js
* @return {number[]}
*/
var countBits = function(n) {
const dp = [0];
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.

dp로 구현하진 못했지만 dp를 써야한다는 건 알았던 걸까요?!

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 doitchuu/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.

제 풀이와 비슷하네요ㅎ

@doitchuu doitchuu requested a review from sik9252 March 23, 2026 10:23
sik9252
sik9252 previously approved these changes Mar 25, 2026
Copy link
Copy Markdown
Collaborator

@sik9252 sik9252 left a comment

Choose a reason for hiding this comment

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

둘 다 로직이 짧고 간단해서 직관적으로 읽힌다는 강점이 있는 것 같아요, Counting Bits 푸는 다른 방법도 알아갑니다

@doitchuu doitchuu dismissed stale reviews from sik9252 and raejun92 via 3be8f85 March 26, 2026 23:18
@doitchuu doitchuu merged commit 1716587 into pnt-fe-study:main Mar 30, 2026
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