forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-a-food-rating-system.cpp
33 lines (29 loc) · 1.12 KB
/
design-a-food-rating-system.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
// Time: ctor: O(nlogn)
// changeRating: O(logn)
// highestRated: O(1)
// Space: O(n)
// bst
class FoodRatings {
public:
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
for (int i = 0; i < size(ratings); ++i) {
food_to_cuisine_[foods[i]] = cuisines[i];
food_to_rating_[foods[i]] = ratings[i];
cuisine_to_rating_foods_[cuisines[i]].emplace(-ratings[i], foods[i]);
}
}
void changeRating(string food, int newRating) {
const int old_rating = food_to_rating_[food];
const auto& cuisine = food_to_cuisine_[food];
cuisine_to_rating_foods_[cuisine].erase({-old_rating, food});
food_to_rating_[food] = newRating;
cuisine_to_rating_foods_[cuisine].emplace(-newRating, food);
}
string highestRated(string cuisine) {
return cbegin(cuisine_to_rating_foods_[cuisine])->second;
}
private:
unordered_map<string, string> food_to_cuisine_;
unordered_map<string, int> food_to_rating_;
unordered_map<string, set<pair<int, string>>> cuisine_to_rating_foods_;
};