diff --git a/oh-chaeyeon/139_Word_Break.js b/oh-chaeyeon/139_Word_Break.js new file mode 100644 index 0000000..022de5f --- /dev/null +++ b/oh-chaeyeon/139_Word_Break.js @@ -0,0 +1,30 @@ +/** + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +var wordBreak = function (s, wordDict) { + const wordSet = new Set(wordDict); + const dp = new Array(s.length + 1).fill(false); + dp[0] = true; + + for (let i = 1; i <= s.length; i++) { + for (const word of wordSet) { + const wordLength = word.length; + if ( + i >= wordLength && + dp[i - wordLength] && + s.substring(i - wordLength, i) === word + ) { + dp[i] = true; + break; + } + } + } + + return dp[s.length]; +}; + +// Time taken : 12m 49s +// Runtime : 3ms +// Memory : 51.24MB diff --git a/oh-chaeyeon/1584_Min_Cost_to_Connect_all_points.js b/oh-chaeyeon/1584_Min_Cost_to_Connect_all_points.js new file mode 100644 index 0000000..7ca304b --- /dev/null +++ b/oh-chaeyeon/1584_Min_Cost_to_Connect_all_points.js @@ -0,0 +1,41 @@ +/** + * @param {number[][]} points + * @return {number} + */ +var minCostConnectPoints = function (points) { + const n = points.length; + const dist = new Array(n).fill(Infinity); + const visited = new Array(n).fill(false); + dist[0] = 0; + let cost = 0; + + for (let i = 0; i < n; i++) { + let minCost = Infinity; + let currentNode = -1; + + for (let j = 0; j < n; j++) { + if (!visited[j] && dist[j] < minCost) { + minCost = dist[j]; + currentNode = j; + } + } + + visited[currentNode] = true; + cost += minCost; + + for (let nextNode = 0; nextNode < n; nextNode++) { + if (!visited[nextNode]) { + const manhattanDistance = + Math.abs(points[currentNode][0] - points[nextNode][0]) + + Math.abs(points[currentNode][1] - points[nextNode][1]); + dist[nextNode] = Math.min(dist[nextNode], manhattanDistance); + } + } + } + + return cost; +}; + +// Time taken : 1h+ +// Runtime : 33ms +// Memory : 52.37MB diff --git a/oh-chaeyeon/215_Kth largest_element_in_an_Array.js b/oh-chaeyeon/215_Kth largest_element_in_an_Array.js new file mode 100644 index 0000000..01dc25a --- /dev/null +++ b/oh-chaeyeon/215_Kth largest_element_in_an_Array.js @@ -0,0 +1,46 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var findKthLargest = function (nums, k) { + const maxHeap = new MaxPriorityQueue(); + + for (const num of nums) { + maxHeap.enqueue(num); + } + + for (let i = 0; i < k - 1; i++) { + maxHeap.dequeue(); + } + return maxHeap.front().element; +}; + +// Time taken : 16m 50s +// Runtime : 148ms +// Memory : 77.60MB + +//---------------------------------------------------- + +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var findKthLargest = function (nums, k) { + const offset = 10000; + const count = new Array(20001).fill(0); + + for (let num of nums) { + count[num + offset]++; + } + + let total = 0; + for (let i = count.length - 1; i >= 0; i--) { + total += count[i]; + if (total >= k) return i - offset; + } +}; + +// Runtime : 5ms +// Memory : 58.40MB diff --git a/oh-chaeyeon/235_Lowest_Common_ancestor_of_a_Binary_Search_Tree.js b/oh-chaeyeon/235_Lowest_Common_ancestor_of_a_Binary_Search_Tree.js new file mode 100644 index 0000000..32182d3 --- /dev/null +++ b/oh-chaeyeon/235_Lowest_Common_ancestor_of_a_Binary_Search_Tree.js @@ -0,0 +1,30 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ + +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function (root, p, q) { + while (root) { + if (p.val < root.val && q.val < root.val) { + root = root.left; + } else if (p.val > root.val && q.val > root.val) { + root = root.right; + } else { + return root; + } + } + return null; +}; + +// Time taken : 9m 37s +// Runtime : 56ms +// Memory : 58.70MB diff --git a/oh-chaeyeon/56_Merge_Intervals.js b/oh-chaeyeon/56_Merge_Intervals.js new file mode 100644 index 0000000..5b518dd --- /dev/null +++ b/oh-chaeyeon/56_Merge_Intervals.js @@ -0,0 +1,29 @@ +/** + * @param {number[][]} intervals + * @return {number[][]} + */ +var merge = function (intervals) { + if (intervals.length === 0) return []; + intervals.sort((a, b) => a[0] - b[0]); + + const merged = [intervals[0]]; + + for (let i = 1; i < intervals.length; i++) { + const current = intervals[i]; + + if (current[0] <= merged[merged.length - 1][1]) { + merged[merged.length - 1][1] = Math.max( + merged[merged.length - 1][1], + current[1] + ); + } else { + merged.push(current); + } + } + + return merged; +}; + +// Time taken : 23m 25s +// Runtime : 7ms +// Memory : 59.04MB