-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jdy8739] week 13 #1067
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
[jdy8739] week 13 #1067
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ce01867
meeting-rooms solution
jdy8739 a0867c7
Merge branch 'DaleStudy:main' into main
jdy8739 bc4ccbb
meeting-roos line fix
jdy8739 a2ec0d6
meeting-rooms linelint
jdy8739 4d5a551
kth-smallest-element-in-a-bst solution
jdy8739 c84c690
lowest-common-ancestor-of-a-binary-search-tree solution
jdy8739 1f862bb
lowest-common-ancestor-of-a-binary-search-tree solution
jdy8739 4d203e9
lowest-common-ancestor-of-a-binary-search-tree solution
jdy8739 98398cc
lowest-common-ancestor-of-a-binary-search-tree solution2
jdy8739 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,37 @@ | ||
/** | ||
* 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} | ||
*/ | ||
var kthSmallest = function(root, k) { | ||
const arr = []; | ||
|
||
const dfs = (node) => { | ||
arr.push(node.val); | ||
|
||
if (node?.left) { | ||
dfs(node.left); | ||
} | ||
|
||
if (node?.right) { | ||
dfs(node.right); | ||
} | ||
} | ||
|
||
dfs(root); | ||
|
||
const sort = arr.sort((a, b) => a - b); | ||
|
||
return sort[k - 1]; | ||
}; | ||
|
||
// 시간복잡도 -> O(nlogn) dfs로 노드의 val을 배열에 넣고 정렬하는 시간이 소요됨 | ||
// 공간복잡도 -> O(n) 리스트의 길이만큼 arr의 공간이 필요함 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 두가지 풀이 방법 모두 넘 좋네요 |
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,76 @@ | ||
/** | ||
* 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} | ||
*/ | ||
var lowestCommonAncestor = function (root, p, q) { | ||
const left = Math.min(p.val, q.val); | ||
const right = Math.max(p.val, q.val); | ||
|
||
const dfs = (node) => { | ||
if (!node) { | ||
return; | ||
} | ||
|
||
if (node.val >= left && node.val <= right) { | ||
return node; | ||
} | ||
|
||
if (node.val > left && node.val > right) { | ||
return dfs(node.left); | ||
} | ||
|
||
if (node.val < left && node.val < right) { | ||
return dfs(node.right); | ||
} | ||
} | ||
|
||
return dfs(root); | ||
}; | ||
|
||
// 시간복잡도 O(logn) -> 이진트리를 이진탐색하면서 트리의 노드를 방문하기떄문(동일한 깊이에서 한 번 왼쪽을 방문하였다면, 그 깊이에서 오른쪽을 방문하지 않음) | ||
// 공간복잡도 O(h) -> 재귀호출을 사용하였으므로 트리의 높이만큼 최대 콜스택의 쌓임이 발생 | ||
|
||
/** | ||
* 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} | ||
*/ | ||
var lowestCommonAncestor = function (root, p, q) { | ||
let node = root; | ||
|
||
while (node) { | ||
if (node.val >= p.val && node.val <= q.val) { | ||
return node; | ||
} else if (node.val < p.val && node.val < q.val) { | ||
node = node.right; | ||
} else if (node.val > p.val && node.val > q.val) { | ||
node = node.left; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return node; | ||
}; | ||
|
||
// 시간복잡도 O(logn) -> 이진트리를 이진탐색하면서 트리의 노드를 방문하기떄문(동일한 깊이에서 한 번 왼쪽을 방문하였다면, 그 깊이에서 오른쪽을 방문하지 않음) | ||
// 공간복잡도 O(1) -> 알고리즘을 위해 요구되는 자료구조나 콜스택의 쌓임이 발생하지 않음 |
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,44 @@ | ||
import { | ||
Interval, | ||
} from '/opt/node/lib/lintcode/index.js'; | ||
|
||
/** | ||
* Definition of Interval: | ||
* class Interval { | ||
* constructor(start, end) { | ||
* this.start = start; | ||
* this.end = end; | ||
* } | ||
* } | ||
*/ | ||
|
||
export class Solution { | ||
/** | ||
* @param intervals: an array of meeting time intervals | ||
* @return: if a person could attend all meetings | ||
*/ | ||
canAttendMeetings(intervals) { | ||
const sort = intervals.sort((a, b) => { | ||
if (a[1] === b[1]) { | ||
return a[0] - b[0]; | ||
} | ||
|
||
return a[1] - b[1]; | ||
}) | ||
|
||
for (let i = 0; i < sort.length - 2; i++) { | ||
const current = sort[i][1]; | ||
const next = sort[i + 1][0]; | ||
|
||
if (current > next) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
// 시간복잡도 O(nlogn) -> sort 함수 사용으로 인한 시간복잡도 | ||
// 공간복잡도 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.
공간 복잡도를 O(�h) h는 재귀스택 만큼의 공간, 으로 풀려면 어떤 방법을 사용할 수 있을까요? :)