-
-
Notifications
You must be signed in to change notification settings - Fork 195
[친환경사과] week13 #1079
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
[친환경사과] week13 #1079
Changes from all commits
Commits
Show all changes
3 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,38 @@ | ||
package leetcode_study | ||
|
||
/** | ||
* binary search tree에서 k 번째 작은 수를 반환하는 문제 | ||
* stack 자료구조를 사용해 중위 순회를 구현합니다. | ||
* | ||
* 시간 복잡도: O(n) or O(logn) | ||
* -> 2번의 loop을 순회하기 때문에 O(n^2)의 시간 복잡도로 판단할 수 있지만 제한된 n 안에서 순회를 진행하기 때문에 O(n)을 넘지 않습니다. | ||
* -> 만약 BST가 균형이 잡혀 있다면 O(logn)의 시간 복잡도를 갖습니다. | ||
* | ||
* 공간 복잡도: O(n) | ||
* -> 탐색한 node를 저장할 stack 공간 | ||
*/ | ||
fun kthSmallest(root: TreeNode?, k: Int): Int { | ||
val stack = ArrayDeque<TreeNode>() | ||
var current = root | ||
var count = 0 | ||
|
||
while (stack.isNotEmpty() || current != null) { | ||
// 왼쪽 자식 노드들을 계속 탐색하여 stack에 추가 | ||
while (current != null) { | ||
stack.addLast(current) | ||
current = current.left | ||
} | ||
|
||
// 가장 왼쪽 노드를 pop | ||
current = stack.removeLast() | ||
count++ | ||
|
||
// k번째 노드라면 값 반환 | ||
if (count == k) return current.`val` | ||
|
||
// 오른쪽 서브트리 탐색 | ||
current = current.right | ||
} | ||
|
||
return -1 // 이론적으로 도달할 수 없는 부분 | ||
} |
24 changes: 24 additions & 0 deletions
24
lowest-common-ancestor-of-a-binary-search-tree/EcoFriendlyAppleSu.kt
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,24 @@ | ||
package leetcode_study | ||
|
||
/** | ||
* Binary Search Tree에서 가장 근접한 공통 조상을 찾는 문제 | ||
* BST의 성질인 부모 노드보다 작은 값은 왼쪽 부모 노드보다 큰 값은 오른쪽에 위치함을 사용해 문제 해결 | ||
* | ||
* 시간 복잡도: O(n) or O(logn) | ||
* -> balanced BST의 경우 조회 구간이 절반으로 줄기 때문에 O(logn)의 시간 복잡도를 갖지만 편향될 경우 O(n)의 시간 복잡도를 가짐. | ||
* | ||
* 공간 복잡도: O(1) | ||
* -> 반복문을 사용하여 추가 메모리를 사용하지 않음 | ||
*/ | ||
fun lowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode? { | ||
var currentNode = root | ||
|
||
while (currentNode != null) { | ||
when { | ||
p?.`val`!! < currentNode.`val` && q?.`val`!! < currentNode.`val` -> currentNode = currentNode.left | ||
p?.`val`!! > currentNode.`val` && q?.`val`!! > currentNode.`val` -> currentNode = currentNode.right | ||
else -> return currentNode | ||
} | ||
} | ||
return null | ||
} |
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. 시간 복잡도를 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,24 @@ | ||
package leetcode_study | ||
|
||
/* | ||
* 겹치는 시간 없이 미팅룸 시간을 잡을 수 있는지 판단하는 문제 | ||
* 시작 시간을 기준으로 정렬한 후 종료 시간과 다음 미팅의 시간을 비교해 겹치는 부분이 있다면 잡을 수 없는 미팅 룸으로 판단 | ||
* | ||
* 시간 복잡도: O(n logn) | ||
* -> intervals null check: O(n) | ||
* -> intervals start 값으로 오름차순 정렬. Timsort 사용: O(n logn) | ||
* -> 겹치는 구간 판단 loop: O(n) | ||
* | ||
* 공간 복잡도: O(n) | ||
* -> 정렬된 새로운 미팅 시간 배열 생성: O(n) | ||
* */ | ||
fun canAttendMeetings(intervals: List<Interval?>): Boolean { | ||
if (intervals.isEmpty()) return true | ||
val sortedByList = intervals.filterNotNull().sortedBy { it.start } | ||
for (i in 1 until sortedByList.size) { | ||
if (sortedByList[i - 1].end > sortedByList[i].start) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
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.
kth smallest element in a bst 문제와 마찬가지로 공간 복잡도는 재귀 스택의 높이가 되어 O(h) 가 되지 않을까요?