-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesign a Food Rating System.cpp
67 lines (51 loc) Β· 1.78 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
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
/*class FoodRatings {
public:
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
}
void changeRating(string food, int newRating) {
}
string highestRated(string cuisine) {
}
};
/**
* 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);
*/
#include <set>
#include <string>
using namespace std;
struct Info {
string name;
string cuisine;
int rating{};
Info() = default;
Info(string name, string cuisine, int rating) : name{name}, cuisine{cuisine}, rating{rating}{}
bool operator<(const Info& first) const {
if (rating == first.rating) {
return name < first.name;
}
return rating > first.rating;
}
};
class FoodRatings {
unordered_map<string, Info> foodNameToInfo;
unordered_map<string, set<Info>> cuisineNameToSortedInfo;
public:
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
for (size_t i = 0; i < foods.size(); ++i) {
foodNameToInfo.emplace(foods[i], Info(foods[i], cuisines[i], ratings[i]));
cuisineNameToSortedInfo[cuisines[i]].emplace(foods[i], cuisines[i], ratings[i]);
}
}
void changeRating(const string& food, int newRating) {
Info& toUpdate = foodNameToInfo[food];
cuisineNameToSortedInfo[toUpdate.cuisine].erase(toUpdate);
toUpdate.rating = newRating;
cuisineNameToSortedInfo[toUpdate.cuisine].insert(toUpdate);
}
string highestRated(const string& cuisine) const {
return cuisineNameToSortedInfo.at(cuisine).begin()->name;
}
};