-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: missing-number, merge-two-sorted-lists solution
- Loading branch information
1 parent
03ded85
commit a18a616
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |