当前位置:网站首页>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());
}
};
边栏推荐
- Do you still have certificates to participate in the open source community?
- Formatdatetime explanation [easy to understand]
- Jmeter——接口测试
- Clickhouse notes 02 -- installation test clickvisual
- Principle analysis of bootloader
- CarSim simulation quick start (16) - ADAS sensor objects of CarSim sensor simulation (2)
- 智能电子界桩自然保护区远程监控解决方案
- 【高等数学】【8】微分方程
- 2022.7.24-----leetcode.1184
- [today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now
猜你喜欢

【高等数学】【3】微分中值定理与导数的应用

Mobile web layout method
![Vulnhub | dc: 6 | [actual combat]](/img/7e/de7d5b56724bde5db2bb8338c35aa8.png)
Vulnhub | dc: 6 | [actual combat]

Network protocol: TCP part2

Socket error Event: 32 Error: 10053. Connection closing...Socket close

"Share" devaxpress asp Net v22.1 latest version system environment configuration requirements

Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework

【单细胞高级绘图】07.KEGG富集结果展示
![[advanced mathematics] [6] differential calculus of multivariate functions](/img/9e/84fe6f74b58cbaabab1b6eed0df556.png)
[advanced mathematics] [6] differential calculus of multivariate functions
![[advanced mathematics] [3] Application of differential mean value theorem and derivative](/img/a9/3b024dbbb201bee4eed6c9f6ce3001.png)
[advanced mathematics] [3] Application of differential mean value theorem and derivative
随机推荐
[cloud native] use of Nacos taskmanager task management
Cloud native guide: what is cloud native infrastructure
网络RTK无人机上机测试[通俗易懂]
QML combines qsqltablemodel to dynamically load data MVC "recommended collection"
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
Google guava is just a brother. What is the real king of caching? (glory Collection Edition)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
CarSim simulation quick start (XIV) - CarSim Simulink joint simulation
Prescan quick start to master the special functions of prescan track editing in lecture 18
Redis source code -ziplist
RF、GBDT、XGboost特征选择方法「建议收藏」
"Share" devaxpress asp Net v22.1 latest version system environment configuration requirements
火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
[advanced mathematics] [4] indefinite integral
Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
redis源码 -ziplist
Apache Mina framework "suggestions collection"
Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
Rand1 generates rand9