-
Notifications
You must be signed in to change notification settings - Fork 3
/
2353.cpp
35 lines (31 loc) · 1.25 KB
/
2353.cpp
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
class FoodRatings {
public:
unordered_map<string, map<int, set<string>>> cuisine2food;
unordered_map<string, pair<int, string>> food2cuisineAndRate;
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
int n = foods.size();
for (int i = 0; i < n; ++i) {
cuisine2food[cuisines[i]][ratings[i]].insert(foods[i]);
food2cuisineAndRate[foods[i]] = make_pair(ratings[i], cuisines[i]);
}
}
void changeRating(string food, int newRating) {
auto [prevRating, cuisine] = food2cuisineAndRate[food];
cuisine2food[cuisine][prevRating].erase(food);
if (cuisine2food[cuisine][prevRating].empty()) {
cuisine2food[cuisine].erase(prevRating);
}
cuisine2food[cuisine][newRating].insert(food);
food2cuisineAndRate[food].first = newRating;
}
string highestRated(string cuisine) {
auto foodSet = cuisine2food[cuisine].rbegin()->second;
return *foodSet.begin();
}
};
/**
* 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);
*/