Skip to content

Commit a18a616

Browse files
committed
feat: missing-number, merge-two-sorted-lists solution
1 parent 03ded85 commit a18a616

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

merge-two-sorted-lists/YeomChaeeun.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
/**
13+
* 두개의 리스트 정렬 - 재귀 알고리즘으로 접근
14+
* 알고리즘 복잡도
15+
* - 시간 복잡도: O(n+m) - 모든 노드를 한 번씩 들르기 때문
16+
* - 공간 복잡도: O(n+m) - 함수 호출 스택이 재귀 호출로 인해 사용하기 때문
17+
* @param list1
18+
* @param list2
19+
*/
20+
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
21+
if(!(list1 && list2)) return list1 || list2
22+
if(list1.val < list2.val) {
23+
list1.next = mergeTwoLists(list1.next, list2);
24+
return list1
25+
} else {
26+
list2.next = mergeTwoLists(list2.next, list1);
27+
return list2
28+
}
29+
}

missing-number/YeomChaeeun.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 주어진 배열의 중간에 없는 숫자 찾기
3+
* 알고리즘 복잡도
4+
* - 시간 복잡도: O(nlogn)
5+
* - 공간 복잡도: O(1)
6+
* @param nums
7+
*/
8+
function missingNumber(nums: number[]): number {
9+
if(nums.length === 1) {
10+
return nums[0] === 0 ? 1 : 0
11+
}
12+
13+
nums.sort((a, b) => a - b)
14+
15+
for(let i = 0; i < nums.length; i++) {
16+
if(nums[0] !== 0) return 0
17+
if(nums[i] + 1 !== nums[i + 1])
18+
return nums[i] + 1
19+
}
20+
}

0 commit comments

Comments
 (0)