2353.设计食物评分系统

Posted by 云起 on 2025-02-28
Estimated Reading Time 1 Minutes
Words 167 In Total
Viewed Times

2353.设计食物评分系统

地址:2353. 设计食物评分系统

思路

分别维护两个hashmap,用set实现有序。

注:set是升序

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class FoodRatings {
public:
map<string, pair<string,int>> foodmap;
map<string, set<pair<int,string>>> topscore;
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
for (int i = 0; i < foods.size(); ++i) {
foodmap[foods[i]] = {cuisines[i],ratings[i]};
topscore[cuisines[i]].insert( { -ratings[i], foods[i]});
}
}
void changeRating(string food, int newRating) {
topscore[foodmap[food].first].erase({ -foodmap[food].second,food });
foodmap[food].second = newRating;
topscore[foodmap[food].first].insert({ -foodmap[food].second, food });

}

string highestRated(string cuisine) {
return topscore[cuisine].begin()->second;
}
};

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !