- Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathY2.java
71 lines (59 loc) · 2.22 KB
/
Y2.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importjava.util.PriorityQueue;
publicclassY2 {
publicstaticStringfavoriteRestaurant(List<String> restaurants_1, List<String> restaurants_2) {
HashMap<String, Integer> hm = newHashMap<>();
intlist1Rank = restaurants_1.size() - 1;
intlist2Rank = restaurants_2.size() - 1;
Stringresult = "Yelpwich";
for (Strings : restaurants_1) {
Integerrank = hm.get(s);
if (rank == null) {
rank = 0;
}
hm.put(s, rank - list1Rank--);
}
for (Strings : restaurants_2) {
Integerrank = hm.get(s);
if (rank == null) {
rank = 0;
}
hm.put(s, rank - list2Rank--);
}
PriorityQueue<Map.Entry<String, Integer>> maxHeap = newPriorityQueue<>((a, b) -> a.getValue() - b.getValue());
for (Map.Entry<String, Integer> entry : hm.entrySet()) {
maxHeap.add(entry);
}
while (!maxHeap.isEmpty()) {
Map.Entry<String, Integer> entry = maxHeap.poll();
if (restaurants_1.contains(entry.getKey()) && restaurants_2.contains(entry.getKey())) {
result = entry.getKey();
break;
}
}
returnresult;
}
publicstaticvoidmain(String[] args) {
// Input One
//
// Ayola,Fresh Rolls,Curry Up Now
// Eatsa,Chez Fayala,Working Girls
//
// Returns "Yelpwich"
List<String> restaurants_1 = List.of("Ayola", "Fresh Rolls", "Curry Up Now");
List<String> restaurants_2 = List.of("Eatsa", "Chez Fayala", "Working Girls");
System.out.println(favoriteRestaurant(restaurants_1, restaurants_2));
// Input Two
//
// El Farolito,Japa Curry,Eatsa
// Japa Curry,Eatsa,Ayola,Working Girls
//
// Returns "Japa Curry"
List<String> restaurants_3 = List.of("El Farolito", "Japa Curry", "Eatsa");
List<String> restaurants_4 = List.of("Japa Curry", "Eatsa,Ayola", "Working Girls");
System.out.println(favoriteRestaurant(restaurants_3, restaurants_4));
return;
}
}