Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Airport {
- public final String name;
- public final String arrivalStringTime;
- public final int arrivalTime;
- Airport(String name, String arrivalStringTime) {
- this.name = name;
- this.arrivalStringTime = arrivalStringTime;
- this.arrivalTime = getTime(arrivalStringTime);
- }
- }
- String flightPlan(String[][] times, String source, String dest) {
- Map<String, List<String[]>> flights = new HashMap<>();
- for (String[] flight : times) {
- flights.putIfAbsent(flight[0], new ArrayList<>());
- flights.putIfAbsent(flight[1], new ArrayList<>());
- flights.get(flight[0]).add(flight);
- }
- if (!flights.containsKey(source)) return "-1";
- PriorityQueue<Airport> queue = new PriorityQueue<>(Comparator.comparingInt(air -> air.arrivalTime));
- queue.add(new Airport(source, "00:00"));
- while (!queue.isEmpty()) {
- Airport node = queue.remove();
- if (node.name.equalsIgnoreCase(dest)) return node.arrivalStringTime;
- for (String[] flight : flights.get(node.name)) {
- if (node.arrivalTime+60 > getTime(flight[2])) continue;
- queue.add(new Airport(flight[1], flight[3]));
- }
- }
- return "-1";
- }
- private int getTime(String time) {
- return Integer.parseInt(time.substring(0,time.indexOf(':'))) * 60 + Integer.parseInt(time.substring(time.indexOf(':')+1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement