Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions maximum-depth-of-binary-tree/seongmin36.js

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Depth-First Search, Binary Search, Dynamic Programming
  • 설명: 코드는 재귀 DFS를 사용해 좌우 자식 노드를 방문하며 트리의 최대 깊이를 계산한다. 각 재귀호출에서 좌우 자식의 깊이를 비교해 최댓값에 1을 더해 부모의 깊이를 구성한다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 트리의 모든 노드를 한 번씩 방문하여 깊이를 계산한다. 재귀 스택의 최대 깊이가 트리의 높이가 된다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
이 문제를 푸는 핵심은 DFS(깊이 우선 탐색)다.
왼쪽으로, 오른쪽으로 쭉 들어가는 값을 반환해서 변수에 저장한다.
이를 재귀호출하면 쉽게 해결할 수 있다.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
function maxDepth(root) {
if (root === null) return 0;

let left_depth = maxDepth(root.left);
let right_depth = maxDepth(root.right);

let count = Math.max(left_depth, right_depth) + 1;

return count;
}
45 changes: 45 additions & 0 deletions merge-two-sorted-lists/seongmin36.js

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming, Greedy, Hash Map / Hash Set
  • 설명: 주요 흐름은 두 링크드 리스트를 배열에 모아 정렬한 뒤, reduceRight로 순차적으로 연결하여 새로운 리스트를 만드는 방식이다. 정렬 과정에서 비교적 전형적인 Greedy/DP 성격의 최적해 보장 패턴은 약하지만, 실제로는 모든 값을 모아 정렬하는 조합/연결 과정으로 두 포인터의 순차 처리와 정렬이 핵심이다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n log n)
Space O(n)

피드백: 배열 및 sort를 사용하므로 추가 공간과 정렬 비용이 발생한다. 리스트를 직접 합치며 정렬하는 방법으로 개선 가능.

개선 제안: 고려해볼 만한 대안: 두 리스트를 한 번 순회하며 병합 정렬 방식으로 O(n) 시간과 O(1) 추가 공간에 가까운 구현으로 개선.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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.

O(1) 공간복잡도와 O(N) 시간복잡도로도 해결하실수 있으세요! 한번 시도해보시는것도 좋겠네요

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
ListNode는 파라미터로 current value와 next value를 지닌다.
배열 메서드도 사용할 수 없다.

나는 원초적인 방법으로 해결해보았다.
result 배열에 모든 수를 다 넣고, sort()하는 것이다.
여기서 중요한 점은 '어떻게 배열 → ListNode로 변경하느냐'이다.
[1, 2, 3] 배열을 1 → 2 → 3 리스트 노드를 만들기 위해서는 컴퓨터 구조상 역방향인 3부터 가져와야한다.
이때 사용한 메서드는 'reduceRight()'다.
reduceRight() 메서드는 reduce() 작업순서의 역방향이다.

TC : O(nLogN) → sort()
SC : O(n)
*/
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
function mergeTwoLists(list1, list2) {
let result = [];
let node1 = list1;
let node2 = list2;

while (node1 !== null) {
result.push(node1.val);
node1 = node1.next;
}

while (node2 !== null) {
result.push(node2.val);
node2 = node2.next;
}

return result
.sort((a, b) => a - b)
.reduceRight((next, val) => new ListNode(val, next), null);
}
Loading