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
| class FoodRatings {
public class Info {
String food;
String cuisine;
int rating;
Info(String food, String cuisine, int rating) {
this.food = food;
this.cuisine = cuisine;
this.rating = rating;
}
}
Map<String, Info> foodMap;
Map<String, PriorityQueue<Info>> cuisineMap;
public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
foodMap = new HashMap<>();
cuisineMap = new HashMap<>();
for(int i = 0; i < foods.length; i++) {
Info info = new Info(foods[i], cuisines[i], ratings[i]);
foodMap.put(foods[i], info);
if(cuisineMap.containsKey(cuisines[i])) {
cuisineMap.get(cuisines[i]).add(info);
}else{
PriorityQueue<Info> pq = new PriorityQueue<Info>(new Comparator<Info>(){
@Override
public int compare(Info a, Info b) {
int result = b.rating - a.rating;
if (result == 0) {
return (a.food).compareTo(b.food);
}
return result;
}
});
pq.add(info);
cuisineMap.put(cuisines[i],pq);
}
}
}
public void changeRating(String food, int newRating) {
Info prev = foodMap.get(food);
Info curr = new Info(food, prev.cuisine, newRating);
foodMap.put(food, curr);
prev.food = "";
cuisineMap.get(prev.cuisine).add(curr);
}
public String highestRated(String cuisine) {
while( cuisineMap.get(cuisine).peek().food.equals("")){
cuisineMap.get(cuisine).remove();
}
return cuisineMap.get(cuisine).peek().food;
}
}
/**
* 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);
*/
|