- Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathReconstruct-Itinerary.kt
33 lines (30 loc) · 915 Bytes
/
Reconstruct-Itinerary.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
packagedepth_first_search
importjava.util.*
importkotlin.collections.HashMap
classReconstructItineraryKotlin332 {
funfindItinerary(tickets:List<List<String>>): List<String> {
val result:MutableList<String> =LinkedList()
if (tickets.isEmpty()) {
return result
}
val map:MutableMap<String, PriorityQueue<String>> =HashMap()
tickets.forEach {
map.computeIfAbsent(it[0]) { PriorityQueue() }.offer(it[1])
}
dfs(map, "JFK", result)
return result
}
privatefundfs(
map:MutableMap<String, PriorityQueue<String>>,
s:String,
result:MutableList<String>
) {
if (map.containsKey(s)) {
val current = map.getValue(s)
while (current.isNotEmpty()) {
dfs(map, current.poll(), result)
}
}
result.add(0, s)
}
}