Skip to content

Commit ada59d3

Browse files
committed
[hackerrank] "Roads and Libraries" 2차 성능 개선
1 parent e140eda commit ada59d3

File tree

1 file changed

+17
-47
lines changed

1 file changed

+17
-47
lines changed
Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package hackerrank.graphs
22

33
import java.util.*
4-
import kotlin.math.min
54

65
/**
76
* isolated subgraphs가 아니라고 가정하고 간단하게 구현
@@ -20,27 +19,22 @@ fun roadsAndLibraries(
2019
): Long {
2120

2221
val nodes = buildCityNodes(n, cities)
23-
val visited = mutableSetOf<CityNode>()
2422
var totalCost = 0L
2523

26-
nodes.forEach { node ->
27-
if (!visited.contains(node)) {
28-
val distance = bfs(node, visited)
24+
0.until(n).forEach { i ->
25+
if (!nodes[i].visited) {
26+
val distance = bfs(nodes[i])
2927

30-
totalCost += calculateMinCost(
31-
distance + 1,
32-
c_lib,
33-
distance,
34-
c_road
35-
)
28+
totalCost += if (c_road < c_lib) distance * c_road + c_lib
29+
else (distance + 1) * c_lib
3630
}
3731
}
3832

3933
return totalCost
4034
}
4135

42-
private fun buildCityNodes(count: Int, cities: Array<Array<Int>>): List<CityNode> {
43-
val nodes = (1..count).map { CityNode(it) }
36+
private fun buildCityNodes(count: Int, cities: Array<Array<Int>>): Array<CityNode> {
37+
val nodes = Array(count) { CityNode() }
4438

4539
cities.forEach { link ->
4640
val from = nodes[link[0] - 1]
@@ -53,57 +47,33 @@ private fun buildCityNodes(count: Int, cities: Array<Array<Int>>): List<CityNode
5347
return nodes
5448
}
5549

56-
private fun bfs(root: CityNode, visited: MutableSet<CityNode>): Int {
50+
private fun bfs(root: CityNode): Int {
5751

5852
var distance = 0
5953
val queue = ArrayDeque<CityNode>()
6054
queue.addLast(root)
6155

6256
while (queue.isNotEmpty()) {
63-
val node = queue.removeFirst()
64-
visited.add(node)
6557

66-
node.neighbors.forEach { neighbor ->
67-
if (!visited.contains(neighbor)) {
58+
val node = queue.removeFirst()
59+
node.visited = true
60+
node.neighbors.forEach {
6861

62+
if (!it.visited) {
6963
distance++
70-
visited.add(neighbor)
71-
queue.addLast(neighbor)
72-
64+
it.visited = true
65+
queue.addLast(it)
7366
}
7467
}
7568
}
7669

7770
return distance
7871
}
7972

80-
private fun calculateMinCost(
81-
cityCount: Int,
82-
c_lib: Int,
83-
distance: Int,
84-
c_road: Int
85-
): Long {
86-
87-
val librariesCost = cityCount * c_lib
88-
val roadsCost = distance * c_road + c_lib
89-
90-
return min(
91-
librariesCost.toLong(),
92-
roadsCost.toLong()
93-
)
94-
}
95-
96-
data class CityNode(val value: Int) {
73+
class CityNode {
9774

75+
var visited = false
9876
val neighbors: MutableSet<CityNode> = mutableSetOf()
99-
100-
fun addNeighbor(neighbor: CityNode): Boolean {
101-
return neighbors.add(neighbor)
102-
}
103-
104-
override fun toString() = "CityNode(" +
105-
"value=$value, " +
106-
"neighbors=${neighbors.map { it.value }}" +
107-
")"
77+
fun addNeighbor(neighbor: CityNode) = neighbors.add(neighbor)
10878

10979
}

0 commit comments

Comments
 (0)