-
-
Notifications
You must be signed in to change notification settings - Fork 194
[hoyeongkwak] Week13 Solutions #1626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,43 @@ | ||
class MedianFinder { | ||
arr: number[] | ||
constructor() { | ||
this.arr = [] | ||
} | ||
/* | ||
Time Complexity: O(logn) | ||
Space Complexity: O(n) | ||
*/ | ||
addNum(num: number): void { | ||
let left = 0 | ||
let right = this.arr.length | ||
|
||
while (left < right) { | ||
const mid = Math.floor((left + right) / 2) | ||
if (this.arr[mid] < num) { | ||
left = mid + 1 | ||
} else { | ||
right = mid | ||
} | ||
} | ||
this.arr.splice(left, 0, num) | ||
} | ||
/* | ||
Time Complexity: O(1) | ||
Space Complexity: O(n) | ||
*/ | ||
findMedian(): number { | ||
const n = this.arr.length | ||
if (n % 2 === 0) { | ||
return (this.arr[n / 2 - 1] + this.arr[n / 2]) / 2 | ||
} else { | ||
return (this.arr[Math.floor(n / 2)]) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Your MedianFinder object will be instantiated and called as such: | ||
* var obj = new MedianFinder() | ||
* obj.addNum(num) | ||
* var param_2 = obj.findMedian() | ||
*/ | ||
This file contains hidden or 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 @@ | ||
/* | ||
Time Complexity: O(n) | ||
Space Complexity: O(n) | ||
*/ | ||
function insert(intervals: number[][], newInterval: number[]): number[][] { | ||
const newIntervals = [] | ||
let idx = 0 | ||
const n = intervals.length | ||
while (idx < n && intervals[idx][1] < newInterval[0]) { | ||
newIntervals.push(intervals[idx]) | ||
idx++ | ||
} | ||
|
||
while (idx < n && intervals[idx][0] <= newInterval[1]) { | ||
newInterval[0] = Math.min(newInterval[0], intervals[idx][0]) | ||
newInterval[1] = Math.max(newInterval[1], intervals[idx][1]) | ||
idx++ | ||
} | ||
newIntervals.push(newInterval) | ||
|
||
while (idx < n) { | ||
newIntervals.push(intervals[idx]) | ||
idx++ | ||
} | ||
return newIntervals | ||
}; |
This file contains hidden or 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,34 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* 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) | ||
* } | ||
* } | ||
*/ | ||
/* | ||
Time Complexity: O(h + k) | ||
Space Complexity: O(h) | ||
*/ | ||
function kthSmallest(root: TreeNode | null, k: number): number { | ||
let count = 0 | ||
let result = -1 | ||
const inOrder = (node: TreeNode | null): void => { | ||
if (node == null || count >= k) return | ||
|
||
inOrder(node.left) | ||
count++ | ||
if (count === k) { | ||
result = node.val | ||
return | ||
} | ||
inOrder(node.right) | ||
} | ||
inOrder(root) | ||
return result | ||
}; |
31 changes: 31 additions & 0 deletions
31
lowest-common-ancestor-of-a-binary-search-tree/hoyeongkwak.ts
This file contains hidden or 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,31 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* 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) | ||
* } | ||
* } | ||
*/ | ||
/* | ||
Time Complexity: O(logn) | ||
Space Complexity: O(1) | ||
*/ | ||
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null { | ||
if (!root || !p || !q) return null; | ||
let node = root | ||
while (node) { | ||
if (p.val < node.val && q.val < node.val) { | ||
node = node.left | ||
} else if (p.val > node.val && q.val > node.val) { | ||
node = node.right | ||
} else { | ||
return node | ||
} | ||
} | ||
return null | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
배열로 하는 방식이 있다는 것을 알게 되었네요
저는 자바라서 자바 내부 클래스 queue를 사용해서 했는데 이렇게 되면 다른 언어에서는 적용이 안되니, 이 방식이 더 효율적일 것 같네요