-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #584 from gitsunmin/main
[gitsunmin] Week 13 Solutions
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
lowest-common-ancestor-of-a-binary-search-tree/gitsunmin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |