-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_2353_DesignAFoodRatingSystem.cpp
81 lines (71 loc) · 2.51 KB
/
LC_2353_DesignAFoodRatingSystem.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
https://leetcode.com/problems/design-a-food-rating-system/
2353. Design a Food Rating System
*/
class FoodRatings {
public:
/*
struct cmpStruct {
bool operator() (pair<int, string> const & lhs, pair<int, string> const & rhs) const
{
if(lhs.first == rhs.first)
return lhs.second<rhs.second;
return lhs.first > rhs.first;
}
};
int n;
unordered_map<string, set<pair<int, string>, cmpStruct>> foodrated;
unordered_map<string, string> foodcus;
unordered_map<string, int> foodrat;
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
n = foods.size();
for(int i=0; i<n; i++)
{
foodcus[foods[i]] = cuisines[i];
foodrat[foods[i]] = ratings[i];
foodrated[cuisines[i]].insert({ratings[i], foods[i]});
// cout<<foods[i]<<" "<<cuisines[i]<<" "<<ratings[i]<<endl;
}
}
void changeRating(string food, int newRating) {
string custype = foodcus[food];
int oldrating = foodrat[food];
foodrated[custype].erase({oldrating, food});
foodrat[food] = newRating;
foodrated[custype].insert({newRating, food});
}
string highestRated(string cuisine) {
auto [rating, food] = *foodrated[cuisine].begin();
return food;
}
*/
unordered_map<string, int> fdratting;
unordered_map<string, string> fdcuisines;
unordered_map<string, set<pair<int, string>>> cuisrated;
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
int n = foods.size();
for(int i=0; i<n; i++)
{
fdratting[foods[i]] = ratings[i];
fdcuisines[foods[i]] = cuisines[i];
cuisrated[cuisines[i]].insert({-ratings[i], foods[i]});
}
}
void changeRating(string food, int newRating) {
int oldrating = fdratting[food];
string cuisinestype = fdcuisines[food];
fdratting[food] = newRating;
cuisrated[cuisinestype].erase({-oldrating, food});
cuisrated[cuisinestype].insert({-newRating, food});
}
string highestRated(string cuisine) {
auto [rating, food] = *cuisrated[cuisine].begin();
return 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);
*/