-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path2353. Design a Food Rating System
57 lines (49 loc) · 1.5 KB
/
2353. Design a Food Rating System
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
struct ComparePairs
{
bool operator()(const pair<int, string> &p1, const pair<int, string> &p2)
{
if (p1.first != p2.first)
return p1.first < p2.first; // food with the highest rating will be at the top
return p1.second > p2.second; // if rating is same, then food with that comes first in alphabetical order gets higher priority
}
};
class FoodRatings
{
public:
unordered_map<string, priority_queue<pair<int, string>, vector<pair<int, string>>, ComparePairs>> m;
unordered_map<string, int> r;
unordered_map<string, string> c;
FoodRatings(vector<string> &foods, vector<string> &cuisines, vector<int> &ratings)
{
for (int i = 0; i < foods.size(); ++i)
{
c[foods[i]] = cuisines[i];
m[cuisines[i]].push({ratings[i], foods[i]});
r[foods[i]] = ratings[i];
}
}
void changeRating(string food, int newr)
{
r[food] = newr;
string cs = c[food];
m[cs].push({newr, food});
}
string highestRated(string cuisine)
{
pair<int, string> p;
while (1)
{
p = m[cuisine].top();
if (r[p.second] != p.first)
m[cuisine].pop();
else
return p.second;
}
}
};
/**
* 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);
*/