Skip to content

[정현준] 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 3 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions maximum-depth-of-binary-tree/jdalma.kt
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)
}
Comment on lines +13 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

간결하고 좋네요!


@Test
fun `노드의 최대 깊이를 반환한다`() {
maxDepth(TreeNode.of(3,9,20,null,null,15,7)) shouldBe 3
}
}
98 changes: 98 additions & 0 deletions reorder-list/jdalma.kt
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)
}
}
}