-
-
Notifications
You must be signed in to change notification settings - Fork 248
[정현준] 11주차 #549
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
[정현준] 11주차 #549
Changes from 1 commit
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,22 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
import kotlin.math.max | ||
|
||
class `maximum-depth-of-binary-tree` { | ||
|
||
/** | ||
* 이진 트리이기에 스택의 깊이가 차지하는 공간 복잡도는 log일 것 | ||
* TC: O(n), SC: O(log n) | ||
*/ | ||
fun maxDepth(root: TreeNode?): Int { | ||
return if (root == null) 0 | ||
else max(maxDepth(root.left) + 1, maxDepth(root.right) + 1) | ||
} | ||
|
||
@Test | ||
fun `노드의 최대 깊이를 반환한다`() { | ||
maxDepth(TreeNode.of(3,9,20,null,null,15,7)) shouldBe 3 | ||
} | ||
} |
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,98 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class `reorder-list` { | ||
|
||
fun reorderList(root: ListNode?) { | ||
if (root == null) return | ||
usingTwoPointers(root) | ||
} | ||
|
||
/** | ||
* TC: O(n), SC: O(n) | ||
*/ | ||
private fun usingStack(input: ListNode) { | ||
val tail = ArrayDeque<ListNode>().apply { | ||
var node: ListNode? = input | ||
while (node != null) { | ||
this.add(node) | ||
node = node.next | ||
} | ||
} | ||
|
||
val dummy = ListNode(-1) | ||
var node: ListNode = dummy | ||
var head: ListNode = input | ||
for (i in 0 until tail.size) { | ||
if (i % 2 != 0) { | ||
node.next = tail.removeLast() | ||
} else { | ||
node.next = head | ||
head.next?.let { head = it } | ||
} | ||
node.next?.let { node = it } | ||
} | ||
node.next = null | ||
} | ||
|
||
/** | ||
* TC: O(n), SC: O(1) | ||
*/ | ||
private fun usingTwoPointers(input: ListNode) { | ||
if (input.next == null) return | ||
|
||
var slow: ListNode? = input | ||
var fast: ListNode? = input | ||
while (fast?.next != null && fast.next?.next != null) { | ||
slow = slow?.next | ||
fast = fast.next?.next | ||
} | ||
|
||
val firstHalfEnd = slow | ||
var secondHalfStart = slow?.next | ||
firstHalfEnd?.next = null | ||
|
||
secondHalfStart = reverse(secondHalfStart) | ||
|
||
var node1: ListNode? = input | ||
var node2: ListNode? = secondHalfStart | ||
while (node2 != null) { | ||
val (next1, next2) = (node1?.next to node2?.next) | ||
node1?.next = node2 | ||
node2.next = next1 | ||
node1 = next1 | ||
node2 = next2 | ||
} | ||
} | ||
|
||
private fun reverse(head: ListNode?): ListNode? { | ||
var prev: ListNode? = null | ||
var current = head | ||
while (current != null) { | ||
val next = current.next | ||
current.next = prev | ||
prev = current | ||
current = next | ||
} | ||
return prev | ||
} | ||
|
||
@Test | ||
fun `정렬된 리스트 노드의 참조 체이닝을 특정 순서로 재정렬한다`() { | ||
val actual = ListNode.of(1,2,3,4,5).apply { | ||
reorderList(this) | ||
} | ||
actual.`val` shouldBe 1 | ||
actual.next!!.`val` shouldBe 5 | ||
actual.next!!.next!!.`val` shouldBe 2 | ||
actual.next!!.next!!.next!!.`val` shouldBe 4 | ||
actual.next!!.next!!.next!!.next!!.`val` shouldBe 3 | ||
actual.next!!.next!!.next!!.next!!.next shouldBe null | ||
|
||
ListNode.of(1,2,3,4,5,6).apply { | ||
reorderList(this) | ||
} | ||
} | ||
} |
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.
간결하고 좋네요!