Skip to content

[친환경사과] 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 3 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions kth-smallest-element-in-a-bst/EcoFriendlyAppleSu.kt
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 // 이론적으로 도달할 수 없는 부분
}
Copy link
Contributor

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) 가 되지 않을까요?

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
}
24 changes: 24 additions & 0 deletions meeting-rooms/EcoFriendlyAppleSu.kt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

시간 복잡도를 O(n) 으로 풀려면 어떻게 하면 좋을지 도전해보시면 좋을것 같습니다 :)

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
}