-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestinationCity.java
More file actions
54 lines (44 loc) · 1.75 KB
/
destinationCity.java
File metadata and controls
54 lines (44 loc) · 1.75 KB
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
48
49
50
51
52
53
54
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class destinationCity {
public static String destCity(List<List<String>> paths) {
Set<String> citySet = new HashSet<>();
for (int i = 0; i < paths.size(); i++) {
citySet.add(paths.get(i).get(0));
}
for (int i = 0; i < paths.size(); i++) {
if (!citySet.contains(paths.get(i).get(1))) {
return paths.get(i).get(1);
}
}
return "-1";
}
public static void main(String[] args) {
List<List<String>> paths1 = new ArrayList<>();
paths1.add(new ArrayList<>(List.of("B","C")));
paths1.add(new ArrayList<>(List.of("D","B")));
paths1.add(new ArrayList<>(List.of("C","A")));
List<List<String>> paths2 = new ArrayList<>();
paths2.add(new ArrayList<>(List.of("London","New York")));
paths2.add(new ArrayList<>(List.of("New York","Lima")));
paths2.add(new ArrayList<>(List.of("Lima","Sao Paulo")));
List<List<String>> paths3 = new ArrayList<>();
paths3.add(new ArrayList<>(List.of("A","Z")));
System.out.println("Destination1: " + destCity(paths1)); // A
System.out.println("Destination2: " + destCity(paths2)); // Sao Paulo
System.out.println("Destination3: " + destCity(paths3)); // Z
}
}
/*
* https://leetcode.com/problems/destination-city/
* Create a set
itereate through paths[i][0];
add paths[i][0] to set
iterate through paths[i][1]
if path[i][1] is in set, keep going
if not in set, return path[i][1]
Time complexity: Loops through twice, O(2n) => O(n)
Space complexity: Created a set, O(n)
* */