Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 45 additions & 0 deletions merge-two-sorted-lists/choidabom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Runtime: 0ms, Memory: 52.30MB
*
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요 상단에 Big O 분석 추가해주시면 더 좋을 것 같습니다 :)

* 접근
* 핵심은 두 리스트를 비교하면서 작은 값부터 정렬되도록 리스트를 만드는 것이다.
* 두 연결 리스트 중 하나가 null이 될 때까지 현재 노드 값을 비교하여 더 작은 값을 새로운 리스트트 추가하고, 남은 리스트를 추가한다.
*
* 평소 접하는 배열이 아닌 링크드 리스트로 풀어야 했기에 접근 방식이 와닿지 않았다.
* 처음에는 list1, list2가 하나의 노드라고 생각하여 헷갈렸지만, 실제로는 각 노드가 next를 통해 연결된 연결 리스트임이 중요하다.
*/

/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function mergeTwoLists(
list1: ListNode | null,
list2: ListNode | null
): ListNode | null {
let dummy = new ListNode(-1);
let current = dummy;

while (list1 !== null && list2 !== null) {
if (list1.val < list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = list2;
list2 = list2.next;
}
current = current.next;
}

current.next = list1 || list2;

return dummy.next;
}
40 changes: 40 additions & 0 deletions missing-number/choidabom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Runtime: 19ms, Memory: 52.48MB
*
* 접근
* 직관적으로 생각했을 때, 0부터 n까지의 숫자 중에서 없는 숫자를 찾아야 한다.
* 완전 탐색으로 정렬한 배열에서 순서대로 비교하면서 없는 숫자를 찾을 수 있다.
* Time Complexity: O(N)
Copy link
Contributor

Choose a reason for hiding this comment

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

ts의 sort()가 어떤 정렬을 사용하는지는 모르지만 보통 nlogn 시간복잡도를 가지고 있다고 알고 있습니다
한번 확인해보시면 좋을것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

V8엔진을 사용하는 경우에 sort() 메서드는 Timsort 알고리즘을 사용한다고 합니다. timsort는 합병 정렬이랑 삽입 정렬을 결합한 알고리즘이네요. 배열을 분할하는 과정에서 O(logn)의 시간 복잡도가 나오고 병합, 비교에서 O(n)의 시간 복잡도라 말씀하신 O(nlogn) 시간 복잡도가 나오는게 맞습니다.
놓친 부분 잘 짚고 넘어갑니다. 리뷰 감사합니다 ~

* Space Complexity: O(N)
*
* Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
*/

function missingNumber(nums: number[]): number {
const numsLen = nums.length;
const sortedNums = nums.sort((a, b) => a - b); // 오름차순 정렬

for (let i = 0; i <= numsLen; i++) {
if (i !== sortedNums[i]) {
return i;
}
}
}

/**
* Runtime: 1ms, Memory: 51.96MB
*
* 접근
* Follow up에 대한 해결 방법
* 0부터 n까지의 숫자의 합을 구한 뒤, 주어진 배열의 합을 빼면 없는 숫자를 찾을 수 있다.
* Time Complexity: O(n)
* Space Complexity: O(1)
*/

function missingNumber(nums: number[]): number {
const size = nums.length;
const sum = (size * (size + 1)) / 2;
const accurate = nums.reduce((sum, num) => sum + num, 0);

return sum - accurate;
}
Loading