generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
62 lines (51 loc) · 1.76 KB
/
solution.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
#include <algorithm>
#include <memory>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
struct Cuisine;
struct Food {
std::string name;
Cuisine* cuisine;
int rating;
Food(const std::string& name, int rating) : name(name), rating(rating) {}
};
struct FoodRating {
Food* food;
bool operator>(const FoodRating& other) const {
if (food->rating == other.food->rating) return food->name < other.food->name;
return food->rating > other.food->rating;
}
};
struct Cuisine {
std::set<FoodRating, std::greater<FoodRating>> foodRatings;
};
class FoodRatings {
private:
std::unordered_map<std::string, std::shared_ptr<Food>> food_map;
std::unordered_map<std::string, std::shared_ptr<Cuisine>> cuisine_map;
public:
FoodRatings(std::vector<std::string>& foods, std::vector<std::string>& cuisines, std::vector<int>& ratings) {
const auto n = std::min({foods.size(), cuisines.size(), ratings.size()});
for (std::size_t i = 0; i < n; ++i) {
auto food = std::make_shared<Food>(foods[i], ratings[i]);
food_map[foods[i]] = food;
auto cuisine_it = cuisine_map.find(cuisines[i]);
if (cuisine_it == cuisine_map.end()) {
cuisine_it = cuisine_map.emplace(cuisines[i], std::make_shared<Cuisine>()).first;
}
cuisine_it->second->foodRatings.insert({.food = food.get()});
food->cuisine = cuisine_it->second.get();
}
}
void changeRating(std::string food_name, int new_rating) {
auto food = food_map[food_name];
food->cuisine->foodRatings.erase({.food = food.get()});
food->rating = new_rating;
food->cuisine->foodRatings.insert({.food = food.get()});
}
std::string highestRated(std::string cuisine) {
return cuisine_map[cuisine]->foodRatings.begin()->food->name;
}
};