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]; +}; 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..8918aa9b7 --- /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) + } +} + +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; +}; 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; +}