Skip to content

Commit 1a881fe

Browse files
authored
Merge pull request #1592 from eunhwa99/main
[eunhwa99] Week 12 solutions
2 parents bf31779 + 32b35a2 commit 1a881fe

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
// TC: O(n log n) - intervals๋ฅผ ์ •๋ ฌํ•˜๋Š”๋ฐ O(n log n)
3+
// SC: O(1) - ์ถ”๊ฐ€์ ์ธ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ
4+
fun eraseOverlapIntervals(intervals: Array<IntArray>): Int {
5+
intervals.sortBy { it[1] } // intervals๋ฅผ ๋๋‚˜๋Š” ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
6+
7+
var prevEnd = intervals[0][1]
8+
var count = 0
9+
for (i in 1 until intervals.size) {
10+
val curStart = intervals[i][0]
11+
if (curStart < prevEnd) { // ๋‘ ๊ตฌ๊ฐ„์ด ๊ฒน์นœ๋‹ค.
12+
count++
13+
} else {
14+
prevEnd = intervals[i][1] // ๊ฒน์น˜์ง€ ์•Š์œผ๋ฉด prevEnd๋ฅผ ๊ฐฑ์‹ 
15+
}
16+
17+
}
18+
return count
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
// Time Complexity: O(n + e)
3+
// Space Complexity: O(n + e)
4+
// n : number of nodes
5+
// e : number of edges
6+
fun countComponents(n: Int, edges: Array<IntArray>): Int {
7+
val graph = Array(n) { mutableListOf<Int>() }
8+
for (edge in edges) { // make graph
9+
graph[edge[0]].add(edge[1])
10+
graph[edge[1]].add(edge[0])
11+
}
12+
val visited = BooleanArray(n) // visited array
13+
var count = 0 // number of connected components
14+
15+
for (i in 0 until n) {
16+
if (!visited[i]) {
17+
dfs(i, graph, visited)
18+
count++
19+
}
20+
}
21+
return count
22+
}
23+
24+
fun dfs(node: Int, graph: Array<MutableList<Int>>, visited: BooleanArray) {
25+
visited[node] = true
26+
for (neighbor in graph[node]) {
27+
if (!visited[neighbor]) {
28+
dfs(neighbor, graph, visited)
29+
}
30+
}
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leetcode_study
2+
3+
class ListNode(var `val`: Int) {
4+
var next: ListNode? = null
5+
}
6+
7+
// ํžŒํŠธ ์ฐธ๊ณ 
8+
// Time Complexity: O(L) where L is the length of the linked list
9+
// Space Complexity: O(1) since we are using only a constant amount of space
10+
class Solution {
11+
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
12+
var left = head
13+
var right = head
14+
var step = n
15+
while (step-- > 0) {
16+
right = right?.next
17+
}
18+
if (right == null) {
19+
return head?.next // n์ด ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด์™€ ๊ฐ™์„ ๋•Œ, ์ฆ‰ ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
20+
}
21+
while (right?.next != null) {
22+
left = left?.next
23+
right = right.next
24+
}
25+
26+
left?.next = left?.next?.next
27+
return head
28+
}
29+
}
30+
31+

โ€Žsame-tree/eunhwa99.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode_study
2+
3+
class TreeNode(var `val`: Int) {
4+
var left: TreeNode? = null
5+
var right: TreeNode? = null
6+
}
7+
8+
// Time Complexity: O(n)
9+
// Space Complexity: O(h) where h is the height of the tree (์žฌ๊ท€ํ•จ์ˆ˜๋กœ ์ธํ•œ ๊ณต๊ฐ„ ๋ณต์žก๋„)
10+
class Solution {
11+
fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean {
12+
return if (p == null && q == null) {
13+
true
14+
} else if (p == null || q == null || p.`val` != q.`val`) {
15+
false
16+
} else {
17+
isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
18+
}
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package leetcode_study
2+
3+
class Codec {
4+
private var index = 0
5+
// Time Complexity: O(n)
6+
// Space Complexity: O(n)
7+
fun serialize(root: TreeNode?): String {
8+
return buildString {
9+
traverse(root, this)
10+
}
11+
}
12+
13+
fun traverse(node: TreeNode?, sb: StringBuilder) {
14+
if (node == null) {
15+
sb.append("null,")
16+
return
17+
}
18+
sb.append("${node.`val`},")
19+
traverse(node.left, sb)
20+
traverse(node.right, sb)
21+
}
22+
23+
// Time Complexity: O(n)
24+
// Space Complexity: O(n)
25+
fun deserialize(data: String): TreeNode? {
26+
val values = data.split(",").filter { it.isNotEmpty() }.toList()
27+
index = 0
28+
return buildTree(values)
29+
}
30+
31+
fun buildTree(values: List<String>): TreeNode? {
32+
if (index >= values.size) return null
33+
val value = values[index++] // index ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ์ „์—ญ์œผ๋กœ ๋‘์–ด์•ผ ํ•œ๋‹ค!!
34+
if (value == "null") return null
35+
val node = TreeNode(value.toInt())
36+
node.left = buildTree(values)
37+
node.right = buildTree(values)
38+
return node
39+
}
40+
}
41+

0 commit comments

Comments
ย (0)