Skip to content

Commit 2e88f27

Browse files
authored
Merge pull request #830 from taewanseoul/main
[Wan] Week 4
2 parents 11eb1fd + d2bc9df commit 2e88f27

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

merge-two-sorted-lists/taewanseoul.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* 21. Merge Two Sorted Lists
3+
* You are given the heads of two sorted linked lists list1 and list2.
4+
* Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
5+
* Return the head of the merged linked list.
6+
*
7+
* https://leetcode.com/problems/merge-two-sorted-lists/description/
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* class ListNode {
13+
* val: number
14+
* next: ListNode | null
15+
* constructor(val?: number, next?: ListNode | null) {
16+
* this.val = (val===undefined ? 0 : val)
17+
* this.next = (next===undefined ? null : next)
18+
* }
19+
* }
20+
*/
21+
22+
// O(n + m) time
23+
// O(n + m) space
24+
function mergeTwoLists(
25+
list1: ListNode | null,
26+
list2: ListNode | null
27+
): ListNode | null {
28+
if (!list1) return list2;
29+
if (!list2) return list1;
30+
31+
let mergedListNode: ListNode;
32+
const val1 = list1.val;
33+
const val2 = list2.val;
34+
if (val1 > val2) {
35+
mergedListNode = new ListNode(val2);
36+
mergedListNode.next = mergeTwoLists(list1, list2.next);
37+
} else {
38+
mergedListNode = new ListNode(val1);
39+
mergedListNode.next = mergeTwoLists(list1.next, list2);
40+
}
41+
42+
return mergedListNode;
43+
}
44+
45+
class ListNode {
46+
val: number;
47+
next: ListNode | null;
48+
constructor(val?: number, next?: ListNode | null) {
49+
this.val = val === undefined ? 0 : val;
50+
this.next = next === undefined ? null : next;
51+
}
52+
}

missing-number/taewanseoul.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 268. Missing Number
3+
* Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
4+
*
5+
* https://leetcode.com/problems/missing-number/description/
6+
*/
7+
8+
// O(n) time
9+
// O(1) space
10+
function missingNumber(nums: number[]): number {
11+
const n = nums.length;
12+
let total = (n * (n + 1)) / 2;
13+
14+
let sum = 0;
15+
for (let i = 0; i < n; i++) {
16+
sum += nums[i];
17+
}
18+
19+
return total - sum;
20+
}

word-search/taewanseoul.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* 268. Missing Number
3+
* Given an m x n grid of characters board and a string word, return true if word exists in the grid.
4+
* The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
5+
*
6+
* https://leetcode.com/problems/word-search/description/
7+
*/
8+
9+
// O(4^n) time
10+
// O(n * m) space
11+
function exist(board: string[][], word: string): boolean {
12+
let result = false;
13+
const num_rows = board.length;
14+
const num_cols = board[0].length;
15+
16+
function checkNeighbors(
17+
board: (string | null)[][],
18+
word: string,
19+
row: number,
20+
col: number,
21+
startIndex: number
22+
) {
23+
const num_rows = board.length;
24+
const num_cols = board[0].length;
25+
26+
if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) return;
27+
28+
if (board[row][col] !== word[startIndex]) return;
29+
30+
if (startIndex === word.length - 1) {
31+
result = true;
32+
return;
33+
}
34+
35+
board[row][col] = null;
36+
checkNeighbors(board, word, row + 1, col, startIndex + 1);
37+
checkNeighbors(board, word, row - 1, col, startIndex + 1);
38+
checkNeighbors(board, word, row, col + 1, startIndex + 1);
39+
checkNeighbors(board, word, row, col - 1, startIndex + 1);
40+
board[row][col] = word[startIndex];
41+
}
42+
43+
for (let i = 0; i < num_rows; i++) {
44+
for (let j = 0; j < num_cols; j++) {
45+
if (board[i][j] === word[0]) {
46+
checkNeighbors(board, word, i, j, 0);
47+
if (result) return result;
48+
}
49+
}
50+
}
51+
52+
return result;
53+
}

0 commit comments

Comments
 (0)