From 599a7d8d523223d8cb41c61313b3106799ef7284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 9 Nov 2024 11:12:12 +0900 Subject: [PATCH 1/4] Add week 13 solutions: meeting-rooms --- meeting-rooms/gitsunmin.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 meeting-rooms/gitsunmin.ts diff --git a/meeting-rooms/gitsunmin.ts b/meeting-rooms/gitsunmin.ts new file mode 100644 index 000000000..f18619871 --- /dev/null +++ b/meeting-rooms/gitsunmin.ts @@ -0,0 +1,26 @@ +/** + * https://www.lintcode.com/problem/920/ + * time complexity : O(n log n) + * space complexity : O(log n) + */ + +export class Interval { + start: number; + end: number; + constructor(start: number, end: number) { + this.start = start; + this.end = end; + } +} +g +export function canAttendMeetings(intervals: Interval[]): boolean { + intervals.sort((a, b) => a.start - b.start); + + for (let i = 0; i < intervals.length - 1; i++) { + const { end } = intervals[i]; + const { start: nextStart } = intervals[i + 1]; + + if (end > nextStart) return false; + } + return true; +} From 33c1b49e3c200c0d6a6c5c081af845aacb194280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 9 Nov 2024 11:30:06 +0900 Subject: [PATCH 2/4] Add week 13 solutions: lowest-common-ancestor-of-a-binary-search-tree --- .../gitsunmin.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts diff --git a/lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts b/lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts new file mode 100644 index 000000000..70ab491d8 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts @@ -0,0 +1,25 @@ +/** + * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ + * time complexity : O(log n) + * space complexity : O(1) + */ + +class TreeNode { + val: number + left: TreeNode | null + right: TreeNode | null + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = (val === undefined ? 0 : val) + this.left = (left === undefined ? null : left) + this.right = (right === undefined ? null : right) + } +} + +function lowestCommonAncestor(root: TreeNode | null, p: TreeNode, q: TreeNode): TreeNode | null { + 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; +}; \ No newline at end of file From 63e5452da4af54eeb947ed51710a9d8adb1e32c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 9 Nov 2024 11:37:48 +0900 Subject: [PATCH 3/4] Add week 13 solutions: house-robber --- house-robber/gitsunmin.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 house-robber/gitsunmin.ts diff --git a/house-robber/gitsunmin.ts b/house-robber/gitsunmin.ts new file mode 100644 index 000000000..80ba45539 --- /dev/null +++ b/house-robber/gitsunmin.ts @@ -0,0 +1,20 @@ +/** + * https://leetcode.com/problems/house-robber/ + * time complexity : O(n) + * space complexity : O(n) + */ + +function rob(nums: number[]): number { + const houseCount = nums.length; + + if (houseCount === 0) return 0; + if (houseCount === 1) return nums[0]; + + const maxRobAmount = new Array(houseCount).fill(0); + maxRobAmount[0] = nums[0]; + maxRobAmount[1] = Math.max(nums[0], nums[1]); + + for (let i = 2; i < houseCount; i++) maxRobAmount[i] = Math.max(maxRobAmount[i - 1], maxRobAmount[i - 2] + nums[i]); + + return maxRobAmount[houseCount - 1]; +}; From 8712becdf318cc5c7406be41c12d3ed40dc91237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 9 Nov 2024 11:41:30 +0900 Subject: [PATCH 4/4] add line break --- lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts b/lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts index 70ab491d8..8918aa9b7 100644 --- a/lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts +++ b/lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts @@ -15,11 +15,11 @@ class TreeNode { } } -function lowestCommonAncestor(root: TreeNode | null, p: TreeNode, q: TreeNode): TreeNode | null { +export function lowestCommonAncestor(root: TreeNode | null, p: TreeNode, q: TreeNode): TreeNode | null { 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; -}; \ No newline at end of file +};