-
-
Notifications
You must be signed in to change notification settings - Fork 245
[친환경사과] Week 11 #1033
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
[친환경사과] Week 11 #1033
Changes from 2 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,28 @@ | ||
package leetcode_study | ||
|
||
/* | ||
* 이진 트리의 최대 깊이를 구하는 문제 | ||
* 재귀를 사용해 문제 해결 | ||
* 시간 복잡도: O(n) | ||
* -> 이진 트리의 모든 노드를 방문 | ||
* 공간 복잡도: O(n) 혹은 O(log n) | ||
* -> findDepth() 함수는 재귀적으로 호출되어 콜 스택에 쌓임 | ||
* -> 균형잡힌 이진트리의 경우 재귀의 깊이는 O(log n) 소요 | ||
* -> 편향된 이진트리의 경우 재귀의 깊이는 O(n) 소요 | ||
* */ | ||
fun maxDepth(root: TreeNode?): Int { | ||
if (root == null) return 0 | ||
val maxValue = findDepth(root, 1) // 시작 깊이 값은 `1` | ||
return maxValue | ||
} | ||
|
||
fun findDepth(currentNode: TreeNode?, depth: Int): Int{ | ||
// escape condition | ||
if (currentNode == null) { | ||
return depth - 1 | ||
} | ||
|
||
val leftValue = findDepth(currentNode.left, depth + 1) | ||
val rightValue = findDepth(currentNode.right, depth + 1) | ||
return maxOf(leftValue, rightValue) | ||
} |
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,36 @@ | ||
package leetcode_study | ||
|
||
/* | ||
* singly linked list를 재정렬하는 문제 | ||
* 시간 복잡도: O(n) | ||
* -> 전체 노드를 저장하기 위해 순환하는 과정 O(n) | ||
* -> 리스트의 앞과 뒤에 포인터를 두고 주소값을 변경하는 과정 O(log n) | ||
* 공간 복잡도: O(n) | ||
* -> 노드 전체를 담을 새로운 list 필요 O(n) | ||
* */ | ||
fun reorderList(head: ListNode?): Unit { | ||
val tempNodeList = mutableListOf<ListNode>() | ||
var currentNode = head | ||
|
||
while (currentNode != null) { | ||
tempNodeList.add(currentNode) | ||
currentNode = currentNode.next | ||
} | ||
|
||
// 양쪽 끝에서부터 교차로 연결 | ||
var i = 0 | ||
var j = tempNodeList.size - 1 | ||
while (i < j) { | ||
// 먼저 앞쪽 노드의 next가 뒤쪽 노드를 가리키게 함 | ||
tempNodeList[i].next = tempNodeList[j] | ||
i++ // 다음 앞쪽 노드 선택 | ||
|
||
// 만약 앞쪽과 뒤쪽이 만난 경우 (짝수개일 때), 반복 종료 | ||
if (i == j) break | ||
|
||
// 뒤쪽 노드의 next가 새로운 앞쪽 노드를 가리키게 함 | ||
tempNodeList[j].next = tempNodeList[i] | ||
j-- // 다음 뒤쪽 노드 선택 | ||
} | ||
tempNodeList[i].next = 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.
오.... dfs에서 return시 depth - 1 이 상당히 참신하고 좋은것 같습니다.