Skip to content

Commit

Permalink
Merge pull request #584 from gitsunmin/main
Browse files Browse the repository at this point in the history
[gitsunmin] Week 13 Solutions
  • Loading branch information
gitsunmin authored Nov 10, 2024
2 parents 552db31 + f527385 commit 7b4cc6b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
20 changes: 20 additions & 0 deletions house-robber/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -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];
};
25 changes: 25 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -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;
};
26 changes: 26 additions & 0 deletions meeting-rooms/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 7b4cc6b

Please sign in to comment.