-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #830 from taewanseoul/main
[Wan] Week 4
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |