-
Notifications
You must be signed in to change notification settings - Fork 0
/
Destination_City.java
31 lines (30 loc) · 1.24 KB
/
Destination_City.java
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
// Leetcode - 1436. Destination City
//You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city.
//It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Destination_City {
public static void main(String[] args) {
List<List<String>> paths = new ArrayList<>(Arrays.asList(
Arrays.asList("London", "New York"),
Arrays.asList("New York", "Lima"),
Arrays.asList("Lima", "Sao Paulo")
));
System.out.println(destCity(paths));
}
public static String destCity(List<List<String>> paths) {
Set<String> set = new HashSet<>();
for(List<String> path : paths){
set.add(path.get(0));
}
for(List<String> path : paths){
if(!set.contains(path.get(1))) {
return path.get(1);
}
}
return null;
}
}