Skip to content

[WEEK05-2] 추슬기#21

Merged
doitchuu merged 2 commits intopnt-fe-study:mainfrom
doitchuu:doitchuu
Mar 16, 2026
Merged

[WEEK05-2] 추슬기#21
doitchuu merged 2 commits intopnt-fe-study:mainfrom
doitchuu:doitchuu

Conversation

@doitchuu
Copy link
Member

@doitchuu doitchuu commented Mar 12, 2026

이렇게 풀었어요

1. Add Binary

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

1) 복잡도 계산

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

2) 접근 아이디어

  1. 문자열의 끝자리부터 더하면서 carry를 관리하는 방식으로 풀려고 했다.
  2. 숫자 변환으로 한 번에 처리하는 방법(parseInt)도 먼저 시도했다.

3) 회고

이번 문제는 결국 못 풀었다 ㅠ
처음엔 단순 변환으로 끝내려고 했는데, 입력 길이가 길어질 때 한계가 있다는 걸 뒤늦게 깨달았다.
정답을 결국 보고 풀어봤는데, 오히려 이런 문제가 더 쥐약인듯..

아래 방식으로 문제를 풀어봤었다.

/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function(a, b) {
  const sum = parseInt(a, 2) + parseInt(b, 2);
  return sum.toString(2);
};
/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function(a, b) {
    const result = [];
    let index = Math.max(a.length, b.length) - 1;
    let carryNumber = 0;

    while (index >= 0) {
        if (!a[index] || !b[index]) {
            const value = a[index] || b[index];
            result.unshift(value);
        }

        const sum = Number(a[index]) + Number(b[index]) + carryNumber;

        if (sum === 2) {
            result.unshift("0");
            carryNumber = 1;
        } else if (sum === 1) {
            result.unshift("1");
            carryNumber = 0;
        } else {
            result.unshift("0");
            carryNumber = 0;
        }

        index--;
    }

    return result.join("");
};
/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function(a, b) {
    const result = [];
    let index = Math.max(a.length, b.length) - 1;
    let carryNumber = 0;

    while (index >= 0) {
        if (!a[index] || !b[index]) {
            const value = a[index] || b[index];
            result.unshift(value);
        }

        const sum = Number(a[index]) + Number(b[index]) + carryNumber;

        if (sum === 2) {
            result.unshift("0");
            carryNumber = 1;
        } else if (sum === 1) {
            result.unshift("1");
            carryNumber = 0;
        } else {
            result.unshift("0");
            carryNumber = 0;
        }

        index--;
    }

    return result.join("");
};


2. Diameter of Binary Tree

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

1) 복잡도 계산

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

2) 접근 아이디어

  1. 각 노드에서 왼쪽/오른쪽 서브트리의 높이를 DFS로 계산했다.
  2. left + right를 해당 노드를 지나는 경로 길이로 보고, 전역 최대값(diameter)을 갱신했다.
  3. 부모로는 현재 노드 기준 높이 max(left, right) + 1만 반환해 재귀를 이어갔다.

3) 회고

처음엔 지름을 "루트를 지나가는 경로"로만 생각해서 잘못 접근했다.
문제는 모든 노드가 지름 후보가 될 수 있다는 점을 놓치면 안 된다는 걸 다시 느꼈다.



Copy link
Collaborator

Choose a reason for hiding this comment

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

저도 처음에 푸신거처럼 parseInt로 했었는데 길이의 한계가 있다해서 찾아보니까 BigInt로 변환하면 된다고 하드라구용 근데 실전에서 이렇게 풀어도 되려나 싶긴하네요ㅠ 그래도 일단 테스트 통과하는게 우선이니

Copy link
Collaborator

Choose a reason for hiding this comment

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

Diameter of Binary Tree도 DFS로 높이를 구하면서 diameter를 갱신하는 전형적인 O(n) 풀이로 잘 작성해주신 것 같아요. 기능적으로는 큰 수정 포인트는 없어 보이고, 전반적으로 코드가 간결해서 읽기 좋았습니다!

Copy link
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.

풀이가 정석 그 자체군요! 고생하셨습니다.

@doitchuu doitchuu merged commit e1192fc into pnt-fe-study:main Mar 16, 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.

4 participants