|
| 1 | +console.log("-------------------------------------------------"); |
| 2 | +console.log("Activity 1: "); |
| 3 | + |
| 4 | +// Task 1: Solve the "Median of Two Sorted Arrays" problem on LeetCode. |
| 5 | +// Write a function that takes two sorted arrays of integers and returns the median of the two sorted arrays. |
| 6 | +// Log the median for a few test cases, including edge cases. |
| 7 | + |
| 8 | +function findMedianSortedArrays(nums1, nums2) { |
| 9 | + const merge = (a, b) => { |
| 10 | + const result = []; |
| 11 | + let i = 0, j = 0; |
| 12 | + while (i < a.length && j < b.length) { |
| 13 | + if (a[i] < b[j]) result.push(a[i++]); |
| 14 | + else result.push(b[j++]); |
| 15 | + } |
| 16 | + return result.concat(a.slice(i)).concat(b.slice(j)); |
| 17 | + }; |
| 18 | + |
| 19 | + const merged = merge(nums1, nums2); |
| 20 | + const len = merged.length; |
| 21 | + if (len % 2 === 0) { |
| 22 | + return (merged[len / 2 - 1] + merged[len / 2]) / 2; |
| 23 | + } else { |
| 24 | + return merged[Math.floor(len / 2)]; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +console.log(findMedianSortedArrays([1, 3], [2])); |
| 29 | +console.log(findMedianSortedArrays([1, 2], [3, 4])); |
| 30 | +console.log(findMedianSortedArrays([0, 0], [0, 0])); |
| 31 | +console.log(findMedianSortedArrays([], [1])); |
| 32 | +console.log(findMedianSortedArrays([2], [])); |
| 33 | + |
| 34 | + |
| 35 | +console.log("-------------------------------------------------"); |
| 36 | +console.log("Activity 2: "); |
| 37 | + |
| 38 | +// Task 2: Solve the "Merge k Sorted Lists" problem on LeetCode. |
| 39 | +// Write a function that takes an array of k linked lists, each sorted in ascending order, and merges them into one sorted linked list. |
| 40 | +// Create a few test cases with linked lists and log the merged list. |
| 41 | + |
| 42 | +class ListNode { |
| 43 | + constructor(val = 0, next = null) { |
| 44 | + this.val = val; |
| 45 | + this.next = next; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function mergeKLists(lists) { |
| 50 | + const mergeTwoLists = (l1, l2) => { |
| 51 | + const dummy = new ListNode(); |
| 52 | + let current = dummy; |
| 53 | + while (l1 !== null && l2 !== null) { |
| 54 | + if (l1.val < l2.val) { |
| 55 | + current.next = l1; |
| 56 | + l1 = l1.next; |
| 57 | + } else { |
| 58 | + current.next = l2; |
| 59 | + l2 = l2.next; |
| 60 | + } |
| 61 | + current = current.next; |
| 62 | + } |
| 63 | + current.next = l1 === null ? l2 : l1; |
| 64 | + return dummy.next; |
| 65 | + }; |
| 66 | + |
| 67 | + if (lists.length === 0) return null; |
| 68 | + |
| 69 | + while (lists.length > 1) { |
| 70 | + const mergedLists = []; |
| 71 | + for (let i = 0; i < lists.length; i += 2) { |
| 72 | + if (i + 1 < lists.length) { |
| 73 | + mergedLists.push(mergeTwoLists(lists[i], lists[i + 1])); |
| 74 | + } else { |
| 75 | + mergedLists.push(lists[i]); |
| 76 | + } |
| 77 | + } |
| 78 | + lists = mergedLists; |
| 79 | + } |
| 80 | + |
| 81 | + return lists[0]; |
| 82 | +} |
| 83 | + |
| 84 | +// functions to create and print linked list |
| 85 | +function createLinkedList(arr) { |
| 86 | + let head = new ListNode(arr[0]); |
| 87 | + let current = head; |
| 88 | + for (let i = 1; i < arr.length; i++) { |
| 89 | + current.next = new ListNode(arr[i]); |
| 90 | + current = current.next; |
| 91 | + } |
| 92 | + return head; |
| 93 | +} |
| 94 | + |
| 95 | +function printLinkedList(head) { |
| 96 | + const result = []; |
| 97 | + while (head) { |
| 98 | + result.push(head.val); |
| 99 | + head = head.next; |
| 100 | + } |
| 101 | + return result; |
| 102 | +} |
| 103 | + |
| 104 | +const list1 = createLinkedList([1, 4, 5]); |
| 105 | +const list2 = createLinkedList([1, 3, 4]); |
| 106 | +const list3 = createLinkedList([2, 6]); |
| 107 | +console.log(printLinkedList(mergeKLists([list1, list2, list3]))); |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +console.log("-------------------------------------------------"); |
| 112 | +console.log("Activity 3: "); |
| 113 | + |
| 114 | +// Task 3: Solve the "Trapping Rain Water" problem on LeetCode. |
| 115 | +// Write a function that takes an array of non-negative integers representing an elevation map, where the width of each bar is 1, and computes how much water it can trap after raining. |
| 116 | +// Log the amount of trapped water for a few test cases. |
| 117 | + |
| 118 | +function trap(height) { |
| 119 | + const n = height.length; |
| 120 | + if (n === 0) return 0; |
| 121 | + |
| 122 | + let left = 0; |
| 123 | + let right = n - 1; |
| 124 | + let leftMax = 0; |
| 125 | + let rightMax = 0; |
| 126 | + let waterTrapped = 0; |
| 127 | + |
| 128 | + while (left <= right) { |
| 129 | + if (height[left] <= height[right]) { |
| 130 | + if (height[left] >= leftMax) { |
| 131 | + leftMax = height[left]; |
| 132 | + } else { |
| 133 | + waterTrapped += leftMax - height[left]; |
| 134 | + } |
| 135 | + left++; |
| 136 | + } else { |
| 137 | + if (height[right] >= rightMax) { |
| 138 | + rightMax = height[right]; |
| 139 | + } else { |
| 140 | + waterTrapped += rightMax - height[right]; |
| 141 | + } |
| 142 | + right--; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return waterTrapped; |
| 147 | +} |
| 148 | + |
| 149 | +console.log(trap([0,1,0,2,1,0,1,3,2,1,2,1])); |
| 150 | +console.log(trap([4,2,0,3,2,5])); |
| 151 | +console.log(trap([1,2,3,4,5,6,7,8,9])); |
| 152 | +console.log(trap([5,4,1,2])); |
| 153 | + |
| 154 | + |
| 155 | +console.log("-------------------------------------------------"); |
| 156 | +console.log("Activity 4: "); |
| 157 | + |
| 158 | +// Task 4: Solve the "N-Queens" problem on LeetCode. |
| 159 | +// Write a function that places n queens on an n x n chessboard such that no two queens attack each other, and returns all distinct solutions to the n-queens puzzle. |
| 160 | +// Log the distinct solutions for a few test cases. |
| 161 | + |
| 162 | +function solveNQueens(n) { |
| 163 | + const result = []; |
| 164 | + const board = Array.from({ length: n }, () => Array(n).fill('.')); |
| 165 | + |
| 166 | + const isValid = (row, col) => { |
| 167 | + for (let i = 0; i < row; i++) { |
| 168 | + if (board[i][col] === 'Q') return false; |
| 169 | + if (col - (row - i) >= 0 && board[i][col - (row - i)] === 'Q') return false; |
| 170 | + if (col + (row - i) < n && board[i][col + (row - i)] === 'Q') return false; |
| 171 | + } |
| 172 | + return true; |
| 173 | + }; |
| 174 | + |
| 175 | + const backtrack = (row) => { |
| 176 | + if (row === n) { |
| 177 | + result.push(board.map(r => r.join(''))); |
| 178 | + return; |
| 179 | + } |
| 180 | + for (let col = 0; col < n; col++) { |
| 181 | + if (isValid(row, col)) { |
| 182 | + board[row][col] = 'Q'; |
| 183 | + backtrack(row + 1); |
| 184 | + board[row][col] = '.'; |
| 185 | + } |
| 186 | + } |
| 187 | + }; |
| 188 | + |
| 189 | + backtrack(0); |
| 190 | + return result; |
| 191 | +} |
| 192 | + |
| 193 | +console.log(solveNQueens(4)); |
| 194 | +console.log(solveNQueens(2)); |
| 195 | +console.log(solveNQueens(3)); |
| 196 | +console.log(solveNQueens(5)); |
| 197 | + |
| 198 | + |
| 199 | +console.log("-------------------------------------------------"); |
| 200 | +console.log("Activity 5: "); |
| 201 | + |
| 202 | +// Task 5: Solve the "Word Ladder" problem on LeetCode. |
| 203 | +// Write a function that takes a begin word, an end word, and a dictionary of words, and finds the length of the shortest transformation sequence from the begin word to the end word. Each transformation can only change one letter, and each transformed word must exist in the word list. |
| 204 | +// Log the length of the shortest transformation sequence for a few test cases. |
| 205 | + |
| 206 | +function ladderLength(beginWord, endWord, wordList) { |
| 207 | + const wordSet = new Set(wordList); |
| 208 | + if (!wordSet.has(endWord)) return 0; |
| 209 | + |
| 210 | + const queue = [[beginWord, 1]]; |
| 211 | + |
| 212 | + while (queue.length > 0) { |
| 213 | + const [word, length] = queue.shift(); |
| 214 | + if (word === endWord) return length; |
| 215 | + |
| 216 | + for (let i = 0; i < word.length; i++) { |
| 217 | + for (let c = 'a'.charCodeAt(0); c <= 'z'.charCodeAt(0); c++) { |
| 218 | + const newWord = word.substring(0, i) + String.fromCharCode(c) + word.substring(i + 1); |
| 219 | + if (wordSet.has(newWord)) { |
| 220 | + wordSet.delete(newWord); |
| 221 | + queue.push([newWord, length + 1]); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + return 0; |
| 228 | +} |
| 229 | + |
| 230 | +console.log(ladderLength("hit", "cog", ["hot","dot","dog","lot","log","cog"])); |
| 231 | +console.log(ladderLength("hit", "cog", ["hot","dot","dog","lot","log"])); |
| 232 | +console.log(ladderLength("hit", "hit", ["hot","dot","dog","lot","log","cog"])); |
| 233 | +console.log(ladderLength("a", "c", ["a", "b", "c"])); |
| 234 | + |
| 235 | +console.log("-------------------------------------------------"); |
0 commit comments