Skip to content

[novacat25] WEEK 04 Solutions - #2965

Open
novacat25 wants to merge 11 commits into
DaleStudy:mainfrom
novacat25:week4
Open

novacat25 wants to merge 11 commits into
DaleStudy:mainfrom
novacat25:week4

Conversation

@novacat25

@novacat25 novacat25 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 Status를 In Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

@dalestudy

dalestudy Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

🤖 AI 리뷰(알고리즘 패턴, 시간/공간 복잡도, 학습 현황) 결과는 Checks 탭에서 볼 수 있어요.

@github-actions github-actions Bot added the ts label Nov 30, 2026
@novacat25 novacat25 changed the title Resolved: merge-two-sorted-lists#224 [novacat25] WEEK 04 Solutions Nov 30, 2026
@novacat25 novacat25 moved this from Solving to In Review in 리트코드 스터디 9기 Dec 1, 2026
Comment on lines +49 to +62
[1차 풀이]
시간 복잡도(TC): O(n)
공간 복잡도(SC): O(h) - h는 Tree의 높이
풀이: 재귀 코드를 활용하여, root 기준으로 left/right를 전개하여 풀어나간다. root가 존재하지 않는 기본값은 0으로, Math.max(left, right) 후 추가로 높이 보정치인 1을 더한다.
*/
// const maxDepth = (root: TreeNode | null): number => {
// const DEFAULT_VALUE = 0

// if (!root) return DEFAULT_VALUE
// let leftHeight = maxDepth(root.left)
// let rightHeight = maxDepth(root.right)

// return Math.max(leftHeight, rightHeight) + 1
// }

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.

1번째 풀이는 재귀dfs로 푸셨네요!

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.

수평을 모두 탐색한 뒤 다음 층으로 넘어가는 BFS 풀이까지 1차 DFS 풀이와 대조해서 보여 주셔서 인상 깊습니다 ㅎㅎ

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 left, right를 사용해서 이진탐색으로 구현한 풀이 잘 봤습니다!
저는 매번 새로운 배열을 만드는 방식으로 진행했는데 풀이대로 하면 공간 복잡도를 줄일 수 있겠네요.

이진 탐색의 조건을 주석으로 적어주신 것도 감사합니다! 공부가 됐습니다.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

문제 조건에서 뜻을 도출하는 것도 중요한 거 같더라고요!
거기서 꽤 많은 힌트가 녹아 있고, 특히 LeetCode는 이 부분을 되게 꼼꼼하게 잡아주는 것 같습니다.
(DP에서 일반 재귀를 쓰면 TLE가 나도록 설계를 했다던지)

Comment on lines +27 to +43
while (!isQueueEmpty(nodeQueue)) {
const levelSize = nodeQueue.length

for (let i = 0; i < levelSize; i++) {
const currentNode = nodeQueue.shift()

if (currentNode?.left) {
nodeQueue.push(currentNode.left)
}

if (currentNode?.right) {
nodeQueue.push(currentNode.right)
}
}

depth++
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 같은 siblings끼리 확인할 때마다 depth를 늘려서 구하는 방식 좋은 것 같습니다!
트리 순회를 방문한 노드로 푸는 방식이 직관적으로 와닿아서 배워갑니다.

tail.next = list2
list2 = list2.next
}
tail = tail.next

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 tail에 계속 조건에 맞는 리스트 노드를 추가하는 방식이 직관적으로 드러나는 것 같아서 좋은 것 같습니다.
작성해주신 코드로 그림 그려보면서 풀이하니까 링크드 리스트에 대한 이해도 되면서 js에서 변수에 값을 할당하는 방식을 한번 더 확인해볼 수 있었습니다.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

이게 Reference 기능을 통해 dummy에 tail을 연결해서 풀 수 있다는 원리가
손풀이나 이론을 알 수 없으면 이해하는데까지 시간이 엄청 걸리긴 하더라고요..!

Comment on lines +30 to +31
for (let i = 0; i < levelSize; i++) {
const currentNode = nodeQueue.shift()

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.

child node가 계속 push되니까 levelSize를 미리 const로 고정해 두고, 그 길이만큼 꺼내서 한 층을 처리한 뒤 depth를 추가하는 원리군요

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

네, 해당 Queue의 levelSize만큼만 shift로 빼고, 나머지는 다음 while 분기에 넘기도록 했습니다.
JS에서 일반 배열로 Queue를 사용할 경우 또 shift() 연산을 통해 이루어지니 이렇게 짰어요!

}
}

const isQueueEmpty = (queue: TreeNode[]) => queue.length === 0

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.

while (nodeQueue.length) 대신 큐가 비어있다는 것을 확인하기 위해 함수를 따로 만드신 것이군요!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

네, 조금 더 가독성에 사치를 부려 봤어요!

Comment on lines +2 to +5
핵심 팁
문제에서 주어지는 list1 = [1,2,4] 및 list2 = [1,3,4]는 그대로 배열을 쓰는 것이 아니다!! (constructor 부분 주목)
const list1 = new ListNode(1, new ListNode(2, new ListNode(4))) // [1] -> [2] -> [4] -> null
const list2 = new ListNode(1, new ListNode(3, new ListNode(4))) // [1] -> [3] -> [4] -> null

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.

팁까지 적어 주셔서 감사합니다 :)
저도 첫 번째 풀이 때는 리스트 그 자체로 보려다 보니 풀이가 길어지고 복잡해졌는데, 덕분에 노드를 직접 연결하는 방식으로 풀 수 있었네요

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

아, 이 부분은 LeetCode가 input sample을 줄 때 Online TS Compiler에서 '이거 어떻게 처리해야 하나?' 하고 막혀있었던 걸 조금 더 확실히 하기 위해 쓴 거에요!

Comment thread merge-two-sorted-lists/novacat25.ts Outdated
const dummyNode = new ListNode(0)
let tail = dummyNode

while (list1 !== null && list2 != null) {

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.

파이썬은 !=만 있는데, 여기는 !==와 != 둘 다 있군요
찾아보니 전자는 타입까지 엄격하게 비교하고, 후자는 타입을 맞춘 뒤 비교하는데 이 문제에서는 list1, list2가 ListNode 아니면 null이라 !=나 !==가 결과가 같네요 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

네, !==가 조금 더 엄격합니다.
다만, 왜 list2 != null 인가? 는..
아마 코드 실수일 가능성이 높습니다.

Comment thread coin-change/novacat25.ts Outdated
}
}

return dp[amount] > amount ? INVALID_ANSWER : dp[amount]

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.

저는 dp 초기값을 큰 수로 설정해서 마지막에 이 초기값과 같은지로 판별했는데, 최소 동전 수가 amount를 넘을 수 없다는 점을 이용해 dp[amount] > amount로 판별하는 방식이 깔끔하네요!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

네, 미탐색 dp[index]는 amount보다 의도적으로 크게 설정시켜서 '값이 될 수 없는 조건'을 의도했습니다!
amount + 1 등의 숫자로 가능이야 하지만, Infinity가 좀 더 직관적일 것 같더라고요.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

3 participants