g0301_0400.s0332_reconstruct_itinerary.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0301_0400.s0332_reconstruct_itinerary
// #Hard #Depth_First_Search #Graph #Eulerian_Circuit
// #2022_11_12_Time_240_ms_(93.88%)_Space_42.6_MB_(91.84%)
import java.util.LinkedList
import java.util.PriorityQueue
class Solution {
fun findItinerary(tickets: List>): List {
val map: HashMap> = HashMap()
val ans = LinkedList()
for (ticket in tickets) {
val src = ticket[0]
val dest = ticket[1]
var pq = map[src]
if (pq == null) {
pq = PriorityQueue()
}
pq.add(dest)
map[src] = pq
}
dfs(map, "JFK", ans)
return ans
}
private fun dfs(map: Map>, src: String, ans: LinkedList) {
val temp = map[src]
while (temp != null && !temp.isEmpty()) {
val nbr = temp.remove()
dfs(map, nbr, ans)
}
ans.addFirst(src)
}
}