From 0c6f9691794769d8fea44dd3bcb4974b61dd1599 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Tue, 5 Nov 2024 11:35:12 +0900 Subject: [PATCH 1/3] 2. Lowest Common Ancestor of a Binary Search Tree --- .../sunjae95.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/sunjae95.js diff --git a/lowest-common-ancestor-of-a-binary-search-tree/sunjae95.js b/lowest-common-ancestor-of-a-binary-search-tree/sunjae95.js new file mode 100644 index 000000000..b39606150 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/sunjae95.js @@ -0,0 +1,31 @@ +/** + * @description + * bfs, dfs와 같은 순회 방법과 treeNode 구조에 child가 아닌 parent라는 속성을 부여해 부모찾기를 아이디어로 접근 + * 하지만 모든 노드를 순회해야하고 p와 q가 속한지점과 둘이 포함하는 관계인지를 중점으로 문제에 접근함 + * 그 결과 postOrder를 생각하게 되어 문제 풀이 + * + * n = length of total treeNode + * time complexity: O(n) + * space complexity: O(n) + */ +var lowestCommonAncestor = function (root, p, q) { + let answer = null; + + const postOrder = (tree) => { + if (tree === null) return [false, false]; + + const [hasLeftP, hasLeftQ] = postOrder(tree.left); + const [hasRightP, hasRightQ] = postOrder(tree.right); + + const hasP = hasLeftP || hasRightP || tree.val === p.val; + const hasQ = hasLeftQ || hasRightQ || tree.val === q.val; + + if (hasP && hasQ && answer === null) answer = tree; + + return [hasP, hasQ]; + }; + + postOrder(root); + + return answer; +}; From 582f53218a96cfcd440c9e2255cf415a1e33cbcd Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Wed, 6 Nov 2024 00:31:57 +0900 Subject: [PATCH 2/3] 3. House Robber --- house-robber/sunjae95.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 house-robber/sunjae95.js diff --git a/house-robber/sunjae95.js b/house-robber/sunjae95.js new file mode 100644 index 000000000..423ff127f --- /dev/null +++ b/house-robber/sunjae95.js @@ -0,0 +1,22 @@ +/** + * @description + * 최대한 많은 양의 돈이라는 문구에서 dynamic programming을 연상 + * 연속된 집은 털 수 없다라는 문구에서 점화식을 도출 할 수 있었음 + * + * n = length of nums + * time complexity: O(n) + * space complexity: O(n) + */ +var rob = function (nums) { + if (nums.length === 1) return nums[0]; + + const dp = Array(nums.length).fill(0); + + dp[0] = nums[0]; + dp[1] = Math.max(nums[1], dp[0]); + + for (let i = 2; i < nums.length; i++) + dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); + + return dp[nums.length - 1]; +}; From c01442276bb0c7cd7728d6b4056fbe9d4c3fbb3c Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Wed, 6 Nov 2024 01:18:05 +0900 Subject: [PATCH 3/3] 4.Non-overlapping Intervals --- non-overlapping-intervals/sunjae95.js | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 non-overlapping-intervals/sunjae95.js diff --git a/non-overlapping-intervals/sunjae95.js b/non-overlapping-intervals/sunjae95.js new file mode 100644 index 000000000..7da591d64 --- /dev/null +++ b/non-overlapping-intervals/sunjae95.js @@ -0,0 +1,32 @@ +/** + * @description + * overlapping이 안되기위한 기준이 필요함을 느낌 + * 처음에는 시작점, 끝점을 기준으로 정렬했지만 16번 테스트에서 실패 + * 정렬기준이 끝점, 시작점 순으로 정렬해야한다고 깨닫게 됨 + * + * n = length of intervals + * time complexity: O(n log n) + * space complexity: O(n) + */ +var eraseOverlapIntervals = function (intervals) { + intervals.sort((a, b) => { + if (a[1] !== b[1]) return a[1] - b[1]; + if (a[0] !== b[0]) return b[0] - a[0]; + return 0; + }); + + let answer = 0; + const current = intervals[0]; + + for (let i = 1; i < intervals.length; i++) { + const [start, end] = intervals[i]; + + if (current[1] > start) answer++; + else { + current[0] = start; + current[1] = end; + } + } + + return answer; +};