From 8a0c699359fe2c25ad2b994330bcd7b26be27a6f Mon Sep 17 00:00:00 2001 From: Taewan Lim Date: Fri, 3 Jan 2025 00:26:15 +0900 Subject: [PATCH 1/4] merge-two-sorted-lists --- merge-two-sorted-lists/taewanseoul.ts | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 merge-two-sorted-lists/taewanseoul.ts diff --git a/merge-two-sorted-lists/taewanseoul.ts b/merge-two-sorted-lists/taewanseoul.ts new file mode 100644 index 000000000..1fdd3f964 --- /dev/null +++ b/merge-two-sorted-lists/taewanseoul.ts @@ -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(1) 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; + } +} From 1793dd618b1aaa605c6c2262f15878f8373a690f Mon Sep 17 00:00:00 2001 From: Taewan Lim Date: Fri, 3 Jan 2025 00:57:16 +0900 Subject: [PATCH 2/4] missing-number --- missing-number/taewanseoul.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 missing-number/taewanseoul.ts diff --git a/missing-number/taewanseoul.ts b/missing-number/taewanseoul.ts new file mode 100644 index 000000000..8a69b91d1 --- /dev/null +++ b/missing-number/taewanseoul.ts @@ -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; +} From 2ed2557ae6dd33a7ceb4491a4cfcbeda6cfc45b8 Mon Sep 17 00:00:00 2001 From: Taewan Lim Date: Fri, 3 Jan 2025 22:05:32 +0900 Subject: [PATCH 3/4] word-search --- word-search/taewanseoul.ts | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 word-search/taewanseoul.ts diff --git a/word-search/taewanseoul.ts b/word-search/taewanseoul.ts new file mode 100644 index 000000000..6679d0ad8 --- /dev/null +++ b/word-search/taewanseoul.ts @@ -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; +} From d2bc9df61081c40011f9c6f33f2fa085beb57858 Mon Sep 17 00:00:00 2001 From: Taewan Lim Date: Fri, 3 Jan 2025 22:09:03 +0900 Subject: [PATCH 4/4] merge-two-sorted-lists: time complexity adjustment --- merge-two-sorted-lists/taewanseoul.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/merge-two-sorted-lists/taewanseoul.ts b/merge-two-sorted-lists/taewanseoul.ts index 1fdd3f964..ece78b220 100644 --- a/merge-two-sorted-lists/taewanseoul.ts +++ b/merge-two-sorted-lists/taewanseoul.ts @@ -20,7 +20,7 @@ */ // O(n + m) time -// O(1) space +// O(n + m) space function mergeTwoLists( list1: ListNode | null, list2: ListNode | null