当前位置:网站首页>leetcode-6126:设计食物评分系统
leetcode-6126:设计食物评分系统
2022-07-25 20:36:00 【菊头蝙蝠】
leetcode-6126:设计食物评分系统
题目
解题
方法一:有序map+有序set
根据烹饪方式,可以获得菜品和对应的得分,里面选择一个得分最大的。
1.首先使用unordered_map<string,XXXX> 根据烹饪方式,获取对应得菜品和得分。
2.那么如何获得 得分 最大呢,可以使用有序map(红黑树),以得分作为key,从大到小排序。
因此有map<int,XXXX,greater<int>>,于是可以通过得分获得对应的菜品
3.为了保证菜品为字典序,因此使用有序集合,从小到大排序 ,set<string>
于是unordered_map<string,map<int,set<string>,greater<int>>> map;
就可以根据 烹饪方式---->最大得分---->字典序最小的
class FoodRatings {
public:
map<string,int> foodRating;//菜品--->得分
map<string,string> foodClass;//菜品-->烹饪方式
unordered_map<string,map<int,set<string>,greater<int>>> map;//烹饪方式---->得分(从大到小)---->菜品
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
for(int i=0;i<foods.size();i++){
foodRating[foods[i]]=ratings[i];
foodClass[foods[i]]=cuisines[i];
map[cuisines[i]][ratings[i]].insert(foods[i]);
}
}
void changeRating(string food, int newRating) {
//从集合中删除对应的旧菜品得分
map[foodClass[food]][foodRating[food]].erase(food);
if(map[foodClass[food]][foodRating[food]].empty()){
//如果对应得分的map为空,那么就删除该map
map[foodClass[food]].erase(foodRating[food]);
}
//记录新菜品得分
foodRating[food]=newRating;
map[foodClass[food]][newRating].insert(food);
}
string highestRated(string cuisine) {
return *(map[cuisine].begin()->second.begin());
}
};
边栏推荐
- 使用cookie登录百度网盘(网站使用cookie)
- “链”接无限可能:数字资产链,精彩马上来!
- If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
- Network RTK UAV test [easy to understand]
- JMeter - interface test
- RF, gbdt, xgboost feature selection methods "recommended collection"
- QQ是32位还是64位软件(在哪看电脑是32位还是64位)
- Kubernetes advanced part learning notes
- C language file reading and writing
- Fanoutexchange switch code tutorial
猜你喜欢

移动web布局方法

Increase swap space

9. < tag dynamic programming and subsequence, subarray> lt.718. Longest repeated subarray + lt.1143. Longest common subsequence

Jmeter——接口测试

During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?

JVM (XXIII) -- JVM runtime parameters

If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)

Detailed explanation of document operation
![[matlab] download originality documents based on oil monkey script and MATLAB](/img/c2/1788b758778ba73dd02fb0d006869e.png)
[matlab] download originality documents based on oil monkey script and MATLAB

Step num problem
随机推荐
Advantages of network virtualization of various manufacturers
Introduction and construction of consul Registration Center
【TensorRT】trtexec工具转engine
CarSim simulation quick start (XV) - ADAS sensor objects of CarSim sensor simulation (1)
Formatdatetime explanation [easy to understand]
Prescan quick start to master Lesson 19: prescan actuator configuration, track synchronization and non configuration of multiple tracks
数据库清空表数据并让主键从1开始
Vivo official website app full model UI adaptation scheme
The database empties the table data and makes the primary key start from 1
Online random coin tossing positive and negative statistical tool
文件操作详解
增加 swap 空间
每条你收藏的资讯背后,都离不开TA
Has baozi ever played in the multi merchant system?
FormatDateTime说解[通俗易懂]
Introduction to several scenarios involving programming operation of Excel in SAP implementation project
Apache MINA框架「建议收藏」
Increase swap space
10. < tag dynamic programming and subsequence, subarray> lt.53. maximum subarray and + lt.392. Judge subsequence DBC
QQ是32位还是64位软件(在哪看电脑是32位还是64位)