-
-
Notifications
You must be signed in to change notification settings - Fork 245
[uraflower] Week 13 Solutions #1615
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
5 commits
Select commit
Hold shift + click to select a range
9f87153
[ PS ] : Meeting Rooms
uraflower 287c045
[ PS ] : Lowest Common Ancestor of a Binary Search Tree
uraflower 04bdf43
[ PS ] : Kth Smallest Element in a BST
uraflower f45d2fb
[ PS ] : Insert Interval
uraflower 499d198
[ PS ] : Find Median from Data Stream
uraflower 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,72 @@ | ||
// 배열 + 이분 탐색 삽입 (정렬) 로 풀이함 | ||
|
||
// 다른 방식으로는 듀얼 힙을 사용하는 게 있음 (성능 면에서 더 우수함) | ||
// 최소힙, 최대힙 두 개를 두고 나눠서 add 이 때 size 차이는 1 이하여야 함 | ||
// ex. 1 2 3 4 5 6 => 최대 힙에 1 2 3, 최소 힙에 4 5 6 => (3 + 4) / 2 | ||
// ex. 2 3 4 => 최대 힙에 2 3, 최소 힙에 4 => 3 | ||
// 참고로 leetcode에서 JS를 위한 MaxPriorityQueue와 같은 자료구조를 제공하는 듯 한데... | ||
// 메서드를 어디서 볼 수 있는지 모르겠음 | ||
|
||
class MedianFinder { | ||
constructor() { | ||
this.nums = []; | ||
} | ||
|
||
/** | ||
* 시간복잡도: O(n) (이분탐색: log n, splice: n) | ||
* 공간복잡도: O(1) | ||
* @param {number} num | ||
* @return {void} | ||
*/ | ||
addNum(num) { | ||
const i = this.#findInsertPosition(num); | ||
this.nums.splice(i, 0, num); | ||
} | ||
|
||
/** | ||
* 이진 탐색으로 삽입 지점 찾는 함수 | ||
* 시간복잡도: O(log n) | ||
* 공간복잡도: O(1) | ||
*/ | ||
#findInsertPosition(num) { | ||
let left = 0; | ||
let right = this.nums.length; | ||
|
||
while (left <= right) { | ||
const mid = Math.floor((left + right) / 2); | ||
|
||
if (num < this.nums[mid]) { | ||
right = mid - 1; | ||
} else if (this.nums[mid] < num) { | ||
left = mid + 1; | ||
} else { | ||
return mid; | ||
} | ||
} | ||
|
||
return left; | ||
} | ||
|
||
/** | ||
* 시간복잡도: O(1) | ||
* 공간복잡도: O(1) | ||
* @return {number} | ||
*/ | ||
findMedian() { | ||
const len = this.nums.length; | ||
const midIndex = Math.floor(len / 2); | ||
|
||
if (len % 2 === 0) { | ||
return (this.nums[midIndex - 1] + this.nums[midIndex]) / 2; | ||
} else { | ||
return this.nums[midIndex]; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* 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,34 @@ | ||
/** | ||
* @param {number[][]} intervals | ||
* @param {number[]} newInterval | ||
* @return {number[][]} | ||
*/ | ||
const insert = function(intervals, newInterval) { | ||
const merged = []; | ||
let i = 0; | ||
|
||
// 겹치지 않는 앞부분 push | ||
while (i < intervals.length && intervals[i][1] < newInterval[0]) { | ||
merged.push(intervals[i]); | ||
i++; | ||
} | ||
|
||
// 겹치는 부분 처리 | ||
while (i < intervals.length && intervals[i][0] <= newInterval[1]) { | ||
newInterval[0] = Math.min(newInterval[0], intervals[i][0]); | ||
newInterval[1] = Math.max(newInterval[1], intervals[i][1]); | ||
i++; | ||
} | ||
merged.push(newInterval); | ||
|
||
// 겹치지 않는 뒷부분 push | ||
while (i < intervals.length) { | ||
merged.push(intervals[i]); | ||
i++; | ||
} | ||
|
||
return merged; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) (반환할 배열) |
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,29 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val, left, right) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @param {number} k | ||
* @return {number} | ||
*/ | ||
const kthSmallest = function (root, k) { | ||
const sorted = []; | ||
|
||
const traverse = function (root) { | ||
if (!root) return; | ||
traverse(root.left); | ||
sorted.push(root.val); | ||
traverse(root.right); | ||
} | ||
|
||
traverse(root); | ||
return sorted[k - 1]; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) (배열) |
28 changes: 28 additions & 0 deletions
28
lowest-common-ancestor-of-a-binary-search-tree/uraflower.js
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,28 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* @param {TreeNode} root | ||
* @param {TreeNode} p | ||
* @param {TreeNode} q | ||
* @return {TreeNode} | ||
*/ | ||
const lowestCommonAncestor = function (root, p, q) { | ||
// 이진탐색트리(BST)의 특징을 활용하여 | ||
// p, q의 값과 root 값을 비교해 p, q가 속한 트리(left/right)를 판단 | ||
if (p.val < root.val && q.val < root.val) { | ||
return lowestCommonAncestor(root.left, p, q); | ||
} else if (p.val > root.val && q.val > root.val) { | ||
return lowestCommonAncestor(root.right, p, q); | ||
} else { | ||
return root; | ||
} | ||
}; | ||
|
||
// 시간복잡도: O(h) (h: 트리의 높이, 즉 재귀 스택 깊이) | ||
// 공간복잡도: O(h) (h: 트리의 높이, 즉 재귀 스택 깊이) |
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,20 @@ | ||
/** | ||
* @param intervals: an array of meeting time intervals | ||
* @return: if a person could attend all meetings | ||
*/ | ||
const canAttendMeetings = function (intervals) { | ||
intervals.sort((a, b ) => a[0] - b[0]); // 시작 시간 기준으로 오름차순 정렬 | ||
|
||
let prevEnd = intervals[0][1]; | ||
for (const [start, end] of intervals) { | ||
if (start < prevEnd) { | ||
return false; | ||
} | ||
prevEnd = end; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// 시간복잡도: O(n * log n) | ||
// 공간복잡도: O(1) |
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.
저는 Java의 PriorityQueue를 활용해서 풀었는데, 이진 탐색을 이용한 풀이가 인상 깊었습니다. 또 참고 멘트를 통해 JS에서 PriorityQueue와 Heap 자료 구조를 어떻게 적용할 수 있을지에 대해서도 검색해서 공부해볼 수 있는 기회였습니다.