Skip to content

Commit 7868dcb

Browse files
authored
Create 2353. Design a Food Rating System 1 (#888)
2 parents 60b700e + cebf257 commit 7868dcb

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class FoodRatings {
2+
unordered_map<string, string> food_cuisines;
3+
unordered_map<string, int> food_ratings;
4+
struct Node {
5+
int rating;
6+
string food;
7+
};
8+
struct Cmp {
9+
bool operator()(const Node& a, const Node& b) const {
10+
if (a.rating == b.rating) return a.food > b.food;
11+
return a.rating < b.rating;
12+
}
13+
};
14+
unordered_map<string, priority_queue<Node, vector<Node>, Cmp>> cuisines_heap;
15+
16+
public:
17+
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
18+
int n = (int)foods.size();
19+
for (int i = 0; i < n; ++i) {
20+
const string& food = foods[i];
21+
const string& cuisine = cuisines[i];
22+
int rating = ratings[i];
23+
food_cuisines[food] = cuisine;
24+
food_ratings[food] = rating;
25+
cuisines_heap[cuisine].push({rating, food});
26+
}
27+
}
28+
29+
void changeRating(string food, int newRating) {
30+
const string& cuisine = food_cuisines[food];
31+
food_ratings[food] = newRating;
32+
cuisines_heap[cuisine].push({newRating, food});
33+
}
34+
35+
string highestRated(string cuisine) {
36+
auto& pq = cuisines_heap[cuisine];
37+
while (!pq.empty()) {
38+
const auto top = pq.top();
39+
if (food_ratings[top.food] == top.rating) return top.food;
40+
pq.pop();
41+
}
42+
return "";
43+
}
44+
};
45+
46+
/**
47+
* Your FoodRatings object will be instantiated and called as such:
48+
* FoodRatings* obj = new FoodRatings(foods, cuisines, ratings);
49+
* obj->changeRating(food,newRating);
50+
* string param_2 = obj->highestRated(cuisine);
51+
*/

0 commit comments

Comments
 (0)