Conversation
raejun92
approved these changes
Mar 12, 2026
| * @return {string} | ||
| */ | ||
| var addBinary = function (a, b) { | ||
| return (BigInt("0b" + a) + BigInt("0b" + b)).toString(2); |
doitchuu
approved these changes
Mar 14, 2026
Member
doitchuu
left a comment
There was a problem hiding this comment.
코드를 전반적으로 깔끔히 쓰시는군요 👍
지현님 덕에 BigInt 풀이 방식 배워갑니다 💯
| * @return {string} | ||
| */ | ||
| var addBinary = function (a, b) { | ||
| return (BigInt("0b" + a) + BigInt("0b" + b)).toString(2); |
Member
There was a problem hiding this comment.
그래서 parseInt로 풀면 시간 초과로 안되더라구요. BigInt로 풀이하는 방식은 처음 봤는데 데이터 구조를 잘 이용하시네요! 👍
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. Add Binary
1) 복잡도 계산
시간 복잡도: O(n)
공간 복잡도: O(n)
2) 접근 아이디어
처음에 단순히 주어진 a, b를 10진수로 변환한 후 덧셈을 하여 다시 2진수로 변환하면 되겠다는 생각으로
(parseInt(a, 2) + parseInt(b, 2)).toString(2)를 반환하는 코드를 작성했다. 그런데, 일부 Test Case를 통과하지 못했다. 그 이유는 해당 문제에 주어진 최대 크기가 10^4로 최대 10000자리의 2진수인데, 이는 2^10000 수준의 숫자로 자바스크립트의 Number로는 2^53 - 1까지 밖에 정확하게 표현하지 못하기 때문에 표현 불가능한 수준의 숫자인 것이다. 따라서 이 문제는 큰 정수를 직접 처리하거나 BigInt를 사용해야한다.3) 회고
BigInt를 이용해서 풀었지만, 이 문제의 의도는 사실 큰 정수를 직접 처리하라고 낸 문제라고 한다. 그런데 직접 처리하는 방법은 찾지 못해서 결국 다른 사람의 풀이를 봤다. Carry를 고려해 뒤에서부터 계산을 해야하며 투포인터를 응용해야된다고 한다.
2. Diameter of Binary Tree
1) 복잡도 계산
시간 복잡도: O(n)
공간 복잡도: O(n)
2) 접근 아이디어
각 노드를 지나는 가장 긴 경로 길이를 구하는 문제다. 여기서 가장 긴 경로란 왼쪽 서브트리 높이 + 오른쪽 서브트리 높이 중 최댓값을 의미한다.
예를 들어, 아래와 같은 트리가 있다고 가정해보자.
여기서 가장 긴 경로는 4 -> 2 -> 1 -> 3 혹은 5 -> 2 -> 1 -> 3 으로 간선 수는 3이다.
따라서 트리의 모든 노드에 대해
위 과정을 통해 답을 도출할 수 있다.
3) 회고
스스로 해결하지 못해서 다른 사람의 풀이 방법을 참고했다. 이제 점점 어려워지는 것 같다. DFS를 안그래도 잘 못하는데 새로운 유형을 마주하니 어떻게 시작해야할지 떠오르지가 않았다.