Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공간복잡도를 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;
}