Skip to content

Commit

Permalink
Merge pull request #840 from mike2ox/main
Browse files Browse the repository at this point in the history
[moonhyeok] Week 4
  • Loading branch information
SamTheKorean authored Jan 5, 2025
2 parents 931ace2 + be71d9f commit e6f0ea5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
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;
}

0 comments on commit e6f0ea5

Please sign in to comment.