Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[moonhyeok] Week 4 #840

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 43 additions & 0 deletions merge-two-sorted-lists/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* source: https://leetcode.com/problems/merge-two-sorted-lists/
* 풀이방법: 두 리스트를 비교하면서 작은 값을 결과 리스트에 추가
*
* 시간복잡도: O(n + m) (n: list1의 길이, m: list2의 길이)
* 공간복잡도: O(1) (상수 공간만 사용)
*
*/

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 {
const result = new ListNode();
let current = result;
while (list1 !== null && list2 !== null) {
if (list1.val <= list2.val) {
current.next = list1;
list1 = list1.next;
current = current.next;
} else {
current.next = list2;
list2 = list2.next;
current = current.next;
}
}
if (list1 !== null) {
current.next = list1;
}
if (list2 !== null) {
current.next = list2;
}
return result.next; // 처음에 추가한 더미 노드 제외
}
15 changes: 15 additions & 0 deletions missing-number/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* source: https://leetcode.com/problems/missing-number/
* 풀이방법: 0부터 n까지의 합에서 주어진 배열의 합을 빼면 빠진 숫자를 구할 수 있음
*
* 시간복잡도: O(n) (n: nums의 길이)
* 공간복잡도: O(1) (상수 공간만 사용)
*/

function missingNumber(nums: number[]): number {
const n = nums.length;
let expectedSum = (n * (n + 1)) / 2; // 0부터 n까지의 합 공식
let realSum = nums.reduce((sum, cur) => sum + cur, 0);

return expectedSum - realSum;
}
Loading