Skip to content

Commit

Permalink
feat: missing-number, merge-two-sorted-lists solution
Browse files Browse the repository at this point in the history
  • Loading branch information
YeomChaeeun committed Dec 30, 2024
1 parent 03ded85 commit a18a616
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
29 changes: 29 additions & 0 deletions merge-two-sorted-lists/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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)
* }
* }
*/
/**
* 두개의 리스트 정렬 - 재귀 알고리즘으로 접근
* 알고리즘 복잡도
* - 시간 복잡도: O(n+m) - 모든 노드를 한 번씩 들르기 때문
* - 공간 복잡도: O(n+m) - 함수 호출 스택이 재귀 호출로 인해 사용하기 때문
* @param list1
* @param list2
*/
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
if(!(list1 && list2)) return list1 || list2
if(list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1
} else {
list2.next = mergeTwoLists(list2.next, list1);
return list2
}
}
20 changes: 20 additions & 0 deletions missing-number/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 주어진 배열의 중간에 없는 숫자 찾기
* 알고리즘 복잡도
* - 시간 복잡도: O(nlogn)
* - 공간 복잡도: O(1)
* @param nums
*/
function missingNumber(nums: number[]): number {
if(nums.length === 1) {
return nums[0] === 0 ? 1 : 0
}

nums.sort((a, b) => a - b)

for(let i = 0; i < nums.length; i++) {
if(nums[0] !== 0) return 0
if(nums[i] + 1 !== nums[i + 1])
return nums[i] + 1
}
}

0 comments on commit a18a616

Please sign in to comment.