Skip to content

Commit e6f0ea5

Browse files
authored
Merge pull request #840 from mike2ox/main
[moonhyeok] Week 4
2 parents 931ace2 + be71d9f commit e6f0ea5

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* source: https://leetcode.com/problems/merge-two-sorted-lists/
3+
* ํ’€์ด๋ฐฉ๋ฒ•: ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋น„๊ตํ•˜๋ฉด์„œ ์ž‘์€ ๊ฐ’์„ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
4+
*
5+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n + m) (n: list1์˜ ๊ธธ์ด, m: list2์˜ ๊ธธ์ด)
6+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(1) (์ƒ์ˆ˜ ๊ณต๊ฐ„๋งŒ ์‚ฌ์šฉ)
7+
*
8+
*/
9+
10+
class ListNode {
11+
val: number;
12+
next: ListNode | null;
13+
constructor(val?: number, next?: ListNode | null) {
14+
this.val = val === undefined ? 0 : val;
15+
this.next = next === undefined ? null : next;
16+
}
17+
}
18+
19+
function mergeTwoLists(
20+
list1: ListNode | null,
21+
list2: ListNode | null
22+
): ListNode | null {
23+
const result = new ListNode();
24+
let current = result;
25+
while (list1 !== null && list2 !== null) {
26+
if (list1.val <= list2.val) {
27+
current.next = list1;
28+
list1 = list1.next;
29+
current = current.next;
30+
} else {
31+
current.next = list2;
32+
list2 = list2.next;
33+
current = current.next;
34+
}
35+
}
36+
if (list1 !== null) {
37+
current.next = list1;
38+
}
39+
if (list2 !== null) {
40+
current.next = list2;
41+
}
42+
return result.next; // ์ฒ˜์Œ์— ์ถ”๊ฐ€ํ•œ ๋”๋ฏธ ๋…ธ๋“œ ์ œ์™ธ
43+
}

โ€Žmissing-number/mike2ox.tsโ€Ž

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* source: https://leetcode.com/problems/missing-number/
3+
* ํ’€์ด๋ฐฉ๋ฒ•: 0๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ์—์„œ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ํ•ฉ์„ ๋นผ๋ฉด ๋น ์ง„ ์ˆซ์ž๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Œ
4+
*
5+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n) (n: nums์˜ ๊ธธ์ด)
6+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(1) (์ƒ์ˆ˜ ๊ณต๊ฐ„๋งŒ ์‚ฌ์šฉ)
7+
*/
8+
9+
function missingNumber(nums: number[]): number {
10+
const n = nums.length;
11+
let expectedSum = (n * (n + 1)) / 2; // 0๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ ๊ณต์‹
12+
let realSum = nums.reduce((sum, cur) => sum + cur, 0);
13+
14+
return expectedSum - realSum;
15+
}

0 commit comments

Comments
ย (0)