Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wan] Week 4 #830

Merged
merged 4 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading