Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. class Airport {
  2. public final String name;
  3. public final String arrivalStringTime;
  4. public final int arrivalTime;
  5. Airport(String name, String arrivalStringTime) {
  6. this.name = name;
  7. this.arrivalStringTime = arrivalStringTime;
  8. this.arrivalTime = getTime(arrivalStringTime);
  9. }
  10. }
  11. String flightPlan(String[][] times, String source, String dest) {
  12. Map<String, List<String[]>> flights = new HashMap<>();
  13. for (String[] flight : times) {
  14. flights.putIfAbsent(flight[0], new ArrayList<>());
  15. flights.putIfAbsent(flight[1], new ArrayList<>());
  16. flights.get(flight[0]).add(flight);
  17. }
  18. if (!flights.containsKey(source)) return "-1";
  19. PriorityQueue<Airport> queue = new PriorityQueue<>(Comparator.comparingInt(air -> air.arrivalTime));
  20. queue.add(new Airport(source, "00:00"));
  21. while (!queue.isEmpty()) {
  22. Airport node = queue.remove();
  23. if (node.name.equalsIgnoreCase(dest)) return node.arrivalStringTime;
  24. for (String[] flight : flights.get(node.name)) {
  25. if (node.arrivalTime+60 > getTime(flight[2])) continue;
  26. queue.add(new Airport(flight[1], flight[3]));
  27. }
  28. }
  29. return "-1";
  30. }
  31.  
  32. private int getTime(String time) {
  33. return Integer.parseInt(time.substring(0,time.indexOf(':'))) * 60 + Integer.parseInt(time.substring(time.indexOf(':')+1));
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement