当前位置:网站首页>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());
}
};
边栏推荐
- Jmeter——接口测试
- Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework
- 从底层结构开始学习FPGA(16)----PLL/MMCM IP的定制与测试
- [today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
- How much memory does bitmap occupy in the development of IM instant messaging?
- Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
- [today in history] July 1: the father of time-sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world
- tga文件格式(波形声音文件格式)
- [noi simulation] string matching (suffix automata Sam, Mo team, block)
- Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
猜你喜欢

Card link

Key network protocols in tcp/ip four layer model

KEGG通路的从属/注释信息如何获取

Kubernetes advanced part learning notes

4everland storage node portal network design
![Vulnhub | dc: 5 | [actual combat]](/img/c6/34117bbfb83ebdf9e619f4e4590661.png)
Vulnhub | dc: 5 | [actual combat]
![[advanced mathematics] [4] indefinite integral](/img/4f/2aae654599fcc0ee85cb1ba46c9afd.png)
[advanced mathematics] [4] indefinite integral

【高等数学】【8】微分方程

火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
![[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born](/img/0b/73f0d98a6db813e54074abe199ed98.png)
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
随机推荐
Advantages of network virtualization of various manufacturers
“链”接无限可能:数字资产链,精彩马上来!
[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
Prescan quick start to master Lesson 19: prescan actuator configuration, track synchronization and non configuration of multiple tracks
DIY personal server (DIY storage server)
Docker builds redis cluster
数据库清空表数据并让主键从1开始
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
Kubernetes进阶部分学习笔记
网络协议:TCP Part2
[matlab] download originality documents based on oil monkey script and MATLAB
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input
2022.7.24-----leetcode.1184
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
增加 swap 空间
使用cookie登录百度网盘(网站使用cookie)
"Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
Arrow parquet
Introduction to several scenarios involving programming operation of Excel in SAP implementation project