-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.java
75 lines (53 loc) · 2.35 KB
/
Map.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
72
73
74
75
import java.util.HashMap;
import java.util.PriorityQueue;
public class Map {
class FoodRatings {
private HashMap<String , Integer > foodRating ;
private HashMap<String ,String > foodCuisines ;
private HashMap<String , PriorityQueue > AllInfo ;
public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
foodRating = new HashMap<>() ;
foodCuisines = new HashMap<>();
AllInfo = new HashMap<>();
for (int i = 0; i < ratings.length; i++) {
foodRating.put(foods[i], ratings[i]);
foodCuisines.put(cuisines[i], foods[i]);
AllInfo.computeIfAbsent(cuisines[i] , k -> new PriorityQueue<>()).add(new compare(foods[i], ratings[i]));
}
}
public void changeRating(String food, int newRating) {
foodRating.put(food, newRating);
String cuisines = foodCuisines.get(food);
AllInfo.get(cuisines).add(new compare(food, newRating)) ;
}
public String highestRated(String cuisine) {
compare highestRated = (Map.compare) AllInfo.get(cuisine).peek() ;
while (foodRating.get(highestRated.FoodName) != highestRated.FoodRating)) {
AllInfo.get(cuisine).poll();
highestRated = (Map.compare) AllInfo.get(cuisine).peek();
}
return highestRated.FoodName;
}
}
/**
* Your FoodRatings object will be instantiated and called as such:
* FoodRatings obj = new FoodRatings(foods, cuisines, ratings);
* obj.changeRating(food,newRating);
* String param_2 = obj.highestRated(cuisine);
*/
class compare implements Comparable<compare>{
public String FoodName ;
public int FoodRating ;
public compare (String FoodName , int FoodRating ){
this.FoodName = FoodName ;
this.FoodRating = FoodRating ;
}
@Override
public int compareTo(compare next) {
if(next.FoodRating == FoodRating ){
return FoodName.compareTo(next.FoodName);
}
return -1 * Integer.compare(FoodRating, next.FoodRating) ;
}
}
}