Skip to content

Commit cfbe1ce

Browse files
committed
2문제 추가
1 parent 9783cbe commit cfbe1ce

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

graph-valid-tree/jdalma.kt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
6+
class `graph-valid-tree` {
7+
8+
/**
9+
* TC: O(n), SC: O(n)
10+
*/
11+
fun validTree(nodeSize: Int, edges: Array<IntArray>): Boolean {
12+
if (nodeSize - 1 != edges.size) {
13+
return false
14+
}
15+
16+
val visited = mutableSetOf<Int>()
17+
val adj = List(nodeSize) { mutableListOf<Int>() }
18+
for (e in edges) {
19+
adj[e[0]].add(e[1])
20+
adj[e[1]].add(e[0])
21+
}
22+
23+
val queue = ArrayDeque<Int>().apply {
24+
this.add(0)
25+
}
26+
27+
while (queue.isNotEmpty()) {
28+
val now = queue.removeFirst()
29+
visited.add(now)
30+
for (next in adj[now]) {
31+
if (!visited.contains(next)) {
32+
queue.add(next)
33+
}
34+
}
35+
}
36+
37+
return nodeSize == visited.size
38+
}
39+
40+
@Test
41+
fun `노드가 트리의 조건을 만족하는지 여부를 반환한다`() {
42+
validTree(5,
43+
arrayOf(
44+
intArrayOf(0,1),
45+
intArrayOf(0,2),
46+
intArrayOf(0,3),
47+
intArrayOf(1,4)
48+
)
49+
) shouldBe true
50+
51+
validTree(5,
52+
arrayOf(
53+
intArrayOf(0,1),
54+
intArrayOf(0,2),
55+
intArrayOf(0,3),
56+
intArrayOf(1,4),
57+
intArrayOf(2,3),
58+
)
59+
) shouldBe false
60+
}
61+
}

insert-interval/jdalma.kt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class `insert-interval` {
9+
10+
/**
11+
* TC: O(n), SC: O(n)
12+
*/
13+
fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
14+
if (intervals.isEmpty()) return arrayOf(newInterval)
15+
return justIterate(intervals, newInterval)
16+
}
17+
18+
private fun justIterate(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray> {
19+
val result = mutableListOf<IntArray>()
20+
var new: IntArray? = newInterval
21+
22+
for (interval in intervals) {
23+
if (new != null) {
24+
if (new[1] < interval[0]) {
25+
// new 범위가 더 앞에 있다
26+
result.add(new)
27+
result.add(interval)
28+
new = null
29+
} else if (new[0] > interval[1]) {
30+
// new 범위가 더 뒤에 있어 다른 범위에 포함될 가능성이 있음
31+
result.add(interval)
32+
} else {
33+
new[0] = min(new[0], interval[0])
34+
new[1] = max(new[1], interval[1])
35+
}
36+
} else {
37+
result.add(interval)
38+
}
39+
}
40+
if (new != null) {
41+
result.add(new)
42+
}
43+
return result.toTypedArray()
44+
}
45+
46+
@Test
47+
fun name() {
48+
insert(
49+
arrayOf(
50+
intArrayOf(1,2),
51+
intArrayOf(3,5),
52+
intArrayOf(6,7),
53+
intArrayOf(8,10),
54+
intArrayOf(12,16)
55+
),
56+
intArrayOf(4,8)
57+
) shouldBe arrayOf(intArrayOf(1,2), intArrayOf(3,10), intArrayOf(12,16))
58+
}
59+
}

0 commit comments

Comments
 (0)