-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem2353.cpp
94 lines (73 loc) · 2.62 KB
/
problem2353.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
82
83
84
85
86
87
88
89
90
91
92
93
94
// first Solution
class FoodRatings {
using item=pair<int, string>;//(rating, food), (rating, cuisine)
public:
map<string, pair<int, string>>foodRate;
map<string, set<item>>container;
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
for(int i = 0;i < foods.size();i++)
{
foodRate[foods[i]] = {-ratings[i], cuisines[i]};
container[cuisines[i]].insert({-ratings[i], foods[i]});
}
}
void changeRating(string food, int newRating) {
string cuisine = foodRate[food].second;
int rate = foodRate[food].first;
// delete food and his rate then add it again
container[cuisine].erase({rate, food});
container[cuisine].insert({-newRating, food});
// change rate first
foodRate[food].first = -newRating;
}
string highestRated(string cuisine) {
return container[cuisine].begin()->second;
}
};
// second Solution
class custom_comparator
{
public:
bool operator() (const pair<string, int>& a, const pair<string, int>& b) const
{
return a.second == b.second ? a.first < b.first : a.second > b.second;
}
};
class FoodRatings
{
public:
// Hash Function :
// H1(food_name) = { cuisine_name, current_rating }
unordered_map<string, pair<string, int>> food_info;
// Hash Function :
// H2(cuisine_name) = SET({ food_name, current_rating } : ordered by current_rating DESCENDING)
unordered_map<string, set<pair<string, int>, custom_comparator>> cuisine_info;
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) // Time : 0(n)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
const int n = (int) foods.size();
for(int i = 0; i < n; ++i)
{
food_info[foods[i]] = {cuisines[i], ratings[i]};
cuisine_info[cuisines[i]].insert(make_pair(foods[i], ratings[i]));
}
}
void changeRating(string food, int new_rating) // Time : 0(log N)
{
pair<string, int>& f = food_info[food];
const string& cuisine = f.first;
int old_rating = f.second;
set<pair<string, int>, custom_comparator>& st = cuisine_info[cuisine];
// Remove { food, old_rating } record from cuisine_info
st.erase({food, old_rating});
// Update new rating of the food in food_info and its cuisine_info
f.second = new_rating;
st.insert(make_pair(food, new_rating));
return;
}
string highestRated(string cuisine) // Tine : 0(1)
{
return cuisine_info[cuisine].begin()->first;
}
};