Skip to content

[eunhwa99] Week 12 solutions #1592

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 5 commits into from
Jun 21, 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
20 changes: 20 additions & 0 deletions non-overlapping-intervals/eunhwa99.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
// TC: O(n log n) - intervals를 정렬하는데 O(n log n)
// SC: O(1) - 추가적인 공간을 사용하지 않음
fun eraseOverlapIntervals(intervals: Array<IntArray>): Int {
intervals.sortBy { it[1] } // intervals를 끝나는 시간 기준으로 정렬

var prevEnd = intervals[0][1]
var count = 0
for (i in 1 until intervals.size) {
val curStart = intervals[i][0]
if (curStart < prevEnd) { // 두 구간이 겹친다.
count++
} else {
prevEnd = intervals[i][1] // 겹치지 않으면 prevEnd를 갱신
}

}
return count
}
}
32 changes: 32 additions & 0 deletions number-of-connected-components-in-an-undirected-graph/eunhwa99.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
// Time Complexity: O(n + e)
// Space Complexity: O(n + e)
// n : number of nodes
// e : number of edges
fun countComponents(n: Int, edges: Array<IntArray>): Int {
val graph = Array(n) { mutableListOf<Int>() }
for (edge in edges) { // make graph
graph[edge[0]].add(edge[1])
graph[edge[1]].add(edge[0])
}
val visited = BooleanArray(n) // visited array
var count = 0 // number of connected components

for (i in 0 until n) {
if (!visited[i]) {
dfs(i, graph, visited)
count++
}
}
return count
}

fun dfs(node: Int, graph: Array<MutableList<Int>>, visited: BooleanArray) {
visited[node] = true
for (neighbor in graph[node]) {
if (!visited[neighbor]) {
dfs(neighbor, graph, visited)
}
}
}
}
31 changes: 31 additions & 0 deletions remove-nth-node-from-end-of-list/eunhwa99.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package leetcode_study

class ListNode(var `val`: Int) {
var next: ListNode? = null
}

// 힌트 참고
// Time Complexity: O(L) where L is the length of the linked list
// Space Complexity: O(1) since we are using only a constant amount of space
class Solution {
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
var left = head
var right = head
var step = n
while (step-- > 0) {
right = right?.next
}
if (right == null) {
return head?.next // n이 리스트의 길이와 같을 때, 즉 첫 번째 노드를 제거해야 하는 경우
}
while (right?.next != null) {
left = left?.next
right = right.next
}

left?.next = left?.next?.next
return head
}
}


20 changes: 20 additions & 0 deletions same-tree/eunhwa99.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package leetcode_study

class TreeNode(var `val`: Int) {
var left: TreeNode? = null
var right: TreeNode? = null
}

// Time Complexity: O(n)
// Space Complexity: O(h) where h is the height of the tree (재귀함수로 인한 공간 복잡도)
class Solution {
fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean {
return if (p == null && q == null) {
true
} else if (p == null || q == null || p.`val` != q.`val`) {
false
} else {
isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
}
}
}
41 changes: 41 additions & 0 deletions serialize-and-deserialize-binary-tree/eunhwa99.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package leetcode_study

class Codec {
private var index = 0
// Time Complexity: O(n)
// Space Complexity: O(n)
fun serialize(root: TreeNode?): String {
return buildString {
traverse(root, this)
}
}

fun traverse(node: TreeNode?, sb: StringBuilder) {
if (node == null) {
sb.append("null,")
return
}
sb.append("${node.`val`},")
traverse(node.left, sb)
traverse(node.right, sb)
}

// Time Complexity: O(n)
// Space Complexity: O(n)
fun deserialize(data: String): TreeNode? {
val values = data.split(",").filter { it.isNotEmpty() }.toList()
index = 0
return buildTree(values)
}

fun buildTree(values: List<String>): TreeNode? {
if (index >= values.size) return null
val value = values[index++] // index 는 매개변수가 아니라 전역으로 두어야 한다!!
if (value == "null") return null
val node = TreeNode(value.toInt())
node.left = buildTree(values)
node.right = buildTree(values)
return node
}
}