Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy link
Contributor

Choose a reason for hiding this comment

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

타입스크립트는 논리 연산자를 활용하여 간결하게 값 선택 및 조건 처리를 할 수 있는게 좋은 것 같네요!

if(list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1
} else {
list2.next = mergeTwoLists(list2.next, list1);
return list2
}
Comment on lines +22 to +28
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 객체 값을 직접 비교하는 방법을 썼는데 재귀를 쓰니까 새로운 객체를 생성할 필요가 없다는게 좋은 것 같습니다!

}
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++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

오,, 처음엔 순회 범위가 잘못된줄 알았는데요,
혹시 마지막 숫자가 비어있는 경우를 nums[i + 1]undefined가 나오도록 해서 false를 유도하고 nums[i] + 1을 유도하신걸까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵 맞습니다!
[0, 1] 이 예시라면 nums[1] + 1 !== undefined 가 되어서
최종적으론 nums[1] + 1 인 2가 결과값으로 나오게 됩니다!

if(nums[0] !== 0) return 0
if(nums[i] + 1 !== nums[i + 1])
return nums[i] + 1
}
Comment on lines +15 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

nums[i]를 swap하는 방식 혹은 bit연산을 활용해서 O(N)의 시간복잡도로 줄여보는 것도 좋을 것 같아요

}
Loading