-
Notifications
You must be signed in to change notification settings - Fork 0
/
DestinationCity.java
47 lines (41 loc) · 1.4 KB
/
DestinationCity.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package solutions;
import java.util.*;
// [Problem] https://leetcode.com/problems/destination-city
class DestinationCity {
// HashMap
// O(n) time, O(n) space
public String destCity(List<List<String>> paths) {
Map<String, String> cityConnections = new HashMap<>();
for (List<String> path : paths) {
cityConnections.put(path.get(0), path.get(1));
}
for (String city : cityConnections.values()) {
if (!cityConnections.containsKey(city)) {
return city;
}
}
return null;
}
// HashSet
// O(n) time, O(n) space
public String destCityWithSet(List<List<String>> paths) {
Set<String> departureCities = new HashSet<>();
for (List<String> path : paths) {
departureCities.add(path.get(0));
}
for (List<String> path : paths) {
if (!departureCities.contains(path.get(1))) {
return path.get(1);
}
}
return null;
}
// Test
public static void main(String[] args) {
DestinationCity solution = new DestinationCity();
List<List<String>> input = List.of(List.of("B", "C"), List.of("D", "B"), List.of("C", "A"));
String expectedOutput = "A";
String actualOutput = solution.destCity(input);
System.out.println("Test passed? " + expectedOutput.equals(actualOutput));
}
}