|
| 1 | +package com.kishan.scala.leetcode.juneChallenges |
| 2 | + |
| 3 | +import java.util |
| 4 | + |
| 5 | +import scala.collection.JavaConverters._ |
| 6 | +import scala.collection.mutable |
| 7 | + |
| 8 | +/* |
| 9 | +* Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. |
| 10 | +* All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK. |
| 11 | +* |
| 12 | +* Note: |
| 13 | + 1. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. |
| 14 | + For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"]. |
| 15 | + 2. All airports are represented by three capital letters (IATA code). |
| 16 | + 3. You may assume all tickets form at least one valid itinerary. |
| 17 | + 4. One must use all the tickets once and only once. |
| 18 | +* |
| 19 | +* */ |
| 20 | + |
| 21 | +/* |
| 22 | +* Input: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]] |
| 23 | + Output: ["JFK", "MUC", "LHR", "SFO", "SJC"] |
| 24 | +* |
| 25 | +* |
| 26 | +* Input: [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]] |
| 27 | + Output: ["JFK","ATL","JFK","SFO","ATL","SFO"] |
| 28 | + Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. |
| 29 | + But it is larger in lexical order. |
| 30 | +* |
| 31 | +* */ |
| 32 | + |
| 33 | +/* |
| 34 | +* Approach: DFS - Depth First Search |
| 35 | +* |
| 36 | +* 1. We have to build HashMap with Source as Key and Queue of Destination - By Implementing Priority Queue as value. |
| 37 | +* 2. We have to Start from JFK and find the city which doesn't have any more destination and add to linked list at head position. |
| 38 | +* 3. We have to recursively go through each Priority Queue and have to add each destination at the head of Linked List |
| 39 | +* 4. Finally convert LinkedList asScala List and return the result. |
| 40 | +* |
| 41 | +* */ |
| 42 | + |
| 43 | +object ReconstructItinerary { |
| 44 | + private var map: mutable.HashMap[String, mutable.PriorityQueue[String]] = mutable.HashMap() |
| 45 | + private var result: util.LinkedList[String] = new util.LinkedList() |
| 46 | + |
| 47 | + def findItinerary(tickets: List[List[String]]): List[String] = { |
| 48 | + tickets.foreach(a => { |
| 49 | + if (!map.contains(a.head)) { |
| 50 | + var pq: mutable.PriorityQueue[String] = mutable.PriorityQueue.empty[String](Ordering.String.reverse) |
| 51 | + map += (a.head -> pq) |
| 52 | + } |
| 53 | + map(a.head).enqueue(a.last) |
| 54 | + }) |
| 55 | + |
| 56 | + dfs("JFK") |
| 57 | + return result.asScala.toList |
| 58 | + } |
| 59 | + |
| 60 | + def dfs(startCity: String): Unit = { |
| 61 | + var arrivals = map.getOrElse(startCity, null) |
| 62 | + while (arrivals != null && arrivals.nonEmpty) { |
| 63 | + dfs(arrivals.dequeue()) |
| 64 | + } |
| 65 | + result.add(0, startCity) |
| 66 | + } |
| 67 | + |
| 68 | + def main(args: Array[String]): Unit = { |
| 69 | + val input1: List[List[String]] = List(List("MUC", "LHR"), List("JFK", "MUC"), List("SFO", "SJC"), List("LHR", "SFO")) |
| 70 | + val input2: List[List[String]] = List(List("JFK", "SFO"), List("JFK", "ATL"), List("SFO", "ATL"), List("ATL", "JFK"), List("ATL", "SFO")) |
| 71 | + val input3: List[List[String]] = List(List("JFK", "KUL"), List("JFK", "NRT"), List("NRT", "JFK")) |
| 72 | + val input4: List[List[String]] = List(List("EZE", "AXA"), List("TIA", "ANU"), List("ANU", "JFK"), List("JFK", "ANU"), List("ANU", "EZE"), List("TIA", "ANU"), List("AXA", "TIA"), List("TIA", "JFK"), List("ANU", "TIA"), List("JFK", "TIA")) |
| 73 | + val input5: List[List[String]] = List(List("EZE", "TIA"), List("EZE", "HBA"), List("AXA", "TIA"), List("JFK", "AXA"), List("ANU", "JFK"), List("ADL", "ANU"), List("TIA", "AUA"), List("ANU", "AUA"), List("ADL", "EZE"), List("ADL", "EZE"), List("EZE", "ADL"), List("AXA", "EZE"), List("AUA", "AXA"), List("JFK", "AXA"), List("AXA", "AUA"), List("AUA", "ADL"), List("ANU", "EZE"), List("TIA", "ADL"), List("EZE", "ANU"), List("AUA", "ANU")) |
| 74 | + val result = findItinerary(input5) |
| 75 | + println(s"Result: ${result.mkString(",")}, ${result.mkString(",") == "JFK,AXA,AUA,ADL,ANU,AUA,ANU,EZE,ADL,EZE,ANU,JFK,AXA,EZE,TIA,AUA,AXA,TIA,ADL,EZE,HBA"}") |
| 76 | + } |
| 77 | + |
| 78 | + // ["JFK","ANU","EZE","AXA","TIA","ANU","JFK","TIA","ANU","TIA","JFK"] |
| 79 | + // ["JFK","AXA","AUA","ADL","ANU","AUA","ANU","EZE","ADL","EZE","ANU","JFK","AXA","EZE","TIA","AUA","AXA","TIA","ADL","EZE","HBA"] |
| 80 | + |
| 81 | + |
| 82 | +} |
0 commit comments