Skip to content

Commit

Permalink
Merge pull request #830 from taewanseoul/main
Browse files Browse the repository at this point in the history
[Wan] Week 4
  • Loading branch information
SamTheKorean authored Jan 5, 2025
2 parents 11eb1fd + d2bc9df commit 2e88f27
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
52 changes: 52 additions & 0 deletions merge-two-sorted-lists/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 21. Merge Two Sorted Lists
* You are given the heads of two sorted linked lists list1 and list2.
* Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
* Return the head of the merged linked list.
*
* https://leetcode.com/problems/merge-two-sorted-lists/description/
*/

/**
* Definition for singly-linked list.
* 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)
* }
* }
*/

// O(n + m) time
// O(n + m) space
function mergeTwoLists(
list1: ListNode | null,
list2: ListNode | null
): ListNode | null {
if (!list1) return list2;
if (!list2) return list1;

let mergedListNode: ListNode;
const val1 = list1.val;
const val2 = list2.val;
if (val1 > val2) {
mergedListNode = new ListNode(val2);
mergedListNode.next = mergeTwoLists(list1, list2.next);
} else {
mergedListNode = new ListNode(val1);
mergedListNode.next = mergeTwoLists(list1.next, list2);
}

return mergedListNode;
}

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;
}
}
20 changes: 20 additions & 0 deletions missing-number/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 268. Missing Number
* 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.
*
* https://leetcode.com/problems/missing-number/description/
*/

// O(n) time
// O(1) space
function missingNumber(nums: number[]): number {
const n = nums.length;
let total = (n * (n + 1)) / 2;

let sum = 0;
for (let i = 0; i < n; i++) {
sum += nums[i];
}

return total - sum;
}
53 changes: 53 additions & 0 deletions word-search/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* 268. Missing Number
* Given an m x n grid of characters board and a string word, return true if word exists in the grid.
* 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.
*
* https://leetcode.com/problems/word-search/description/
*/

// O(4^n) time
// O(n * m) space
function exist(board: string[][], word: string): boolean {
let result = false;
const num_rows = board.length;
const num_cols = board[0].length;

function checkNeighbors(
board: (string | null)[][],
word: string,
row: number,
col: number,
startIndex: number
) {
const num_rows = board.length;
const num_cols = board[0].length;

if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) return;

if (board[row][col] !== word[startIndex]) return;

if (startIndex === word.length - 1) {
result = true;
return;
}

board[row][col] = null;
checkNeighbors(board, word, row + 1, col, startIndex + 1);
checkNeighbors(board, word, row - 1, col, startIndex + 1);
checkNeighbors(board, word, row, col + 1, startIndex + 1);
checkNeighbors(board, word, row, col - 1, startIndex + 1);
board[row][col] = word[startIndex];
}

for (let i = 0; i < num_rows; i++) {
for (let j = 0; j < num_cols; j++) {
if (board[i][j] === word[0]) {
checkNeighbors(board, word, i, j, 0);
if (result) return result;
}
}
}

return result;
}

0 comments on commit 2e88f27

Please sign in to comment.