Skip to content

Commit b4f2b30

Browse files
authoredJan 4, 2025
Merge pull request #837 from choidabom/main
[choidabom] Week 4
2 parents 33558fa + b6719e5 commit b4f2b30

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
 

‎merge-two-sorted-lists/choidabom.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Runtime: 0ms, Memory: 52.30MB
3+
*
4+
* Time Complexity: O(n)
5+
* Space Complexity: O(1)
6+
*
7+
* 접근
8+
* 핵심은 두 리스트를 비교하면서 작은 값부터 정렬되도록 리스트를 만드는 것이다.
9+
* 두 연결 리스트 중 하나가 null이 될 때까지 현재 노드 값을 비교하여 더 작은 값을 새로운 리스트트 추가하고, 남은 리스트를 추가한다.
10+
*
11+
* 평소 접하는 배열이 아닌 링크드 리스트로 풀어야 했기에 접근 방식이 와닿지 않았다.
12+
* 처음에는 list1, list2가 하나의 노드라고 생각하여 헷갈렸지만, 실제로는 각 노드가 next를 통해 연결된 연결 리스트임이 중요하다.
13+
*/
14+
15+
/**
16+
* Definition for singly-linked list.
17+
* class ListNode {
18+
* val: number
19+
* next: ListNode | null
20+
* constructor(val?: number, next?: ListNode | null) {
21+
* this.val = (val===undefined ? 0 : val)
22+
* this.next = (next===undefined ? null : next)
23+
* }
24+
* }
25+
*/
26+
27+
function mergeTwoLists(
28+
list1: ListNode | null,
29+
list2: ListNode | null
30+
): ListNode | null {
31+
let dummy = new ListNode(-1);
32+
let current = dummy;
33+
34+
while (list1 !== null && list2 !== null) {
35+
if (list1.val < list2.val) {
36+
current.next = list1;
37+
list1 = list1.next;
38+
} else {
39+
current.next = list2;
40+
list2 = list2.next;
41+
}
42+
current = current.next;
43+
}
44+
45+
current.next = list1 || list2;
46+
47+
return dummy.next;
48+
}

‎missing-number/choidabom.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Runtime: 19ms, Memory: 52.48MB
3+
*
4+
* 접근
5+
* 직관적으로 생각했을 때, 0부터 n까지의 숫자 중에서 없는 숫자를 찾아야 한다.
6+
* 완전 탐색으로 정렬한 배열에서 순서대로 비교하면서 없는 숫자를 찾을 수 있다.
7+
* Time Complexity: O(nlogn)
8+
* Space Complexity: O(n)
9+
*
10+
* Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
11+
*/
12+
13+
function missingNumber(nums: number[]): number {
14+
const numsLen = nums.length;
15+
const sortedNums = nums.sort((a, b) => a - b); // 오름차순 정렬
16+
17+
for (let i = 0; i <= numsLen; i++) {
18+
if (i !== sortedNums[i]) {
19+
return i;
20+
}
21+
}
22+
}
23+
24+
/**
25+
* Runtime: 1ms, Memory: 51.96MB
26+
*
27+
* 접근
28+
* Follow up에 대한 해결 방법
29+
* 0부터 n까지의 숫자의 합을 구한 뒤, 주어진 배열의 합을 빼면 없는 숫자를 찾을 수 있다.
30+
* Time Complexity: O(n)
31+
* Space Complexity: O(1)
32+
*/
33+
34+
function missingNumber(nums: number[]): number {
35+
const size = nums.length;
36+
const sum = (size * (size + 1)) / 2;
37+
const accurate = nums.reduce((sum, num) => sum + num, 0);
38+
39+
return sum - accurate;
40+
}

‎word-search/choidabom.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Runtime: 239ms, Memory: 51.98MB
3+
*
4+
* Time Complexity: O(rows * cols * 4^L) L: 단어 길이
5+
* Space Complexity: O(L)
6+
*
7+
*/
8+
function exist(board: string[][], word: string): boolean {
9+
const ROWS = board.length;
10+
const COLUMNS = board[0].length;
11+
12+
for (let r = 0; r < ROWS; r++) {
13+
for (let c = 0; c < COLUMNS; c++) {
14+
if (board[r][c] === word[0]) {
15+
if (check(r, c, 0, board, word)) return true;
16+
}
17+
}
18+
}
19+
20+
return false;
21+
}
22+
23+
function check(
24+
r: number,
25+
c: number,
26+
i: number,
27+
board: string[][],
28+
word: string
29+
): boolean {
30+
const ROWS = board.length;
31+
const COLUMNS = board[0].length;
32+
33+
if (i === word.length) {
34+
return true;
35+
}
36+
37+
if (r < 0 || r >= ROWS || c < 0 || c >= COLUMNS || board[r][c] !== word[i]) {
38+
return false;
39+
}
40+
41+
const temp = board[r][c];
42+
board[r][c] = "#";
43+
44+
const found =
45+
check(r - 1, c, i + 1, board, word) || // 위
46+
check(r, c + 1, i + 1, board, word) || // 오른쪽
47+
check(r + 1, c, i + 1, board, word) || // 아래
48+
check(r, c - 1, i + 1, board, word); // 왼쪽
49+
50+
board[r][c] = temp;
51+
52+
return found;
53+
}

0 commit comments

Comments
 (0)