当前位置:网站首页>6126. Design food scoring system
6126. Design food scoring system
2022-07-24 18:13:00 【hys__ handsome】
Design a food scoring system that supports the following operations :
- modify The score of a certain food listed in the system .
- Return to the food with the highest score under a certain cooking method in the system .
Realization FoodRatings class :
FoodRatings(String[] foods, String[] cuisines, int[] ratings)Initialize system . Food byfoods、cuisinesandratingsdescribe , The length isn.foods[i]It's NoiThe name of the food .cuisines[i]It's NoiHow to cook food .ratings[i]It's NoiThe initial score of the food .
void changeRating(String food, int newRating)Change the name tofoodScore of food .String highestRated(String cuisine)Return to the specified cooking methodcuisineName of the food with the highest score . If there is juxtaposition , return The dictionary order is smaller Name .
Be careful , character string x The dictionary order of is better than that of string y The smaller premise is :x The position that appears in the dictionary is y Before , in other words , or x yes y The prefix of , Or meet x[i] != y[i] The first position i It's about ,x[i] The position appearing in the alphabet is y[i] Before .
Example :
Input
["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
[[["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]], ["korean"], ["japanese"], ["sushi", 16], ["japanese"], ["ramen", 16], ["japanese"]]
Output
[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
explain
FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
foodRatings.highestRated("korean"); // return "kimchi"
// "kimchi" Korean cuisine with the highest score , The score is 9 .
foodRatings.highestRated("japanese"); // return "ramen"
// "ramen" It is the Japanese cuisine with the highest score , The score is 14 .
foodRatings.changeRating("sushi", 16); // "sushi" Now the score is changed to 16 .
foodRatings.highestRated("japanese"); // return "sushi"
// "sushi" It is the Japanese cuisine with the highest score , The score is 16 .
foodRatings.changeRating("ramen", 16); // "ramen" Now the score is changed to 16 .
foodRatings.highestRated("japanese"); // return "ramen"
// "sushi" and "ramen" The scores are all 16 .
// however ,"ramen" The dictionary order of "sushi" smaller .
Tips :
1 <= n <= 2 * 10^4n == foods.length == cuisines.length == ratings.length1 <= foods[i].length, cuisines[i].length <= 10foods[i]、cuisines[i]It's made up of lowercase letters1 <= ratings[i] <= 10^8foodsAll the strings in Different from each other- In the face of
changeRatingIn all calls of ,foodIs the name of the food in the system . - In the face of
highestRatedIn all calls of ,cuisineIt's in the system At least one The way food is cooked . - Call at most
changeRatingandhighestRatedA total of2 * 10^4Time
adopt set Of
Time quick delete 、 Insert 、 Maintain order . Otherwise, every time in highestRated All methods use
Find will timeout , and set Just take the first one .
class FoodRatings {
public:
struct Food{
string food;
int rating;
bool operator <(const Food &t) const {
if(rating != t.rating) return rating > t.rating;
return food < t.food;
}
};
unordered_map<string,set<Food>> um;
unordered_map<string,string> um2;
unordered_map<string,int> um3;
int n = 0;
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
n = foods.size();
for(int i = 0; i < n; i++){
um[cuisines[i]].insert({foods[i],ratings[i]});
um2.insert({foods[i],cuisines[i]});
um3.insert({foods[i],ratings[i]});
}
}
void changeRating(string food, int newRating) { // The main optimization is here
auto i = um[um2[food]].find({food,um3[food]});
if(i != um[um2[food]].end()) um[um2[food]].erase(i);
else cout<<"no found!"<<endl;
um[um2[food]].insert({food,newRating});
um3[food] = newRating;
}
string highestRated(string cuisine) {
return um[cuisine].begin()->food;
}
};
边栏推荐
- Model saving and loading of sklearn
- com.mysql.cj.jdbc.exceptions. MySQLTransactionRollbackException: Deadlock found when trying to get lo
- 0611~自习课
- Inherit, override, overload
- int8 & int8,你栽过这样的跟头吗?
- jmeter --静默运行
- 0623~放假自习
- PXE efficient batch network installation
- Icml2022 Best Paper Award: learning protein reverse folding from millions of predicted structures
- 0614~放假自习
猜你喜欢

What are the pitfalls from single architecture to distributed architecture?

In depth analysis of the famous Alibaba cloud log4j vulnerability

Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery

Icml2022 Best Paper Award: learning protein reverse folding from millions of predicted structures

Inherit, override, overload

Use of jumpserver

After separation, the impression notes are still difficult to live, but there are many coquettish operations

Mozilla foundation released 2022 Internet health report: AI will contribute 15.7 trillion yuan to the global economy in 2030, and the investment in AI in the United States last year was nearly three t

Wechat applet

The use and Simulation of character and string library functions in C language
随机推荐
0623~ holiday self study
Has polardb for PostgreSQL entered the list of Xinchuang database?
Problems needing attention in writing pages
SSM framework learning
How to prepare for hyperinflation
odoo中的bom理解
ES6 cycle filter value
2022最新短视频去水印解析API接口分享
1688/阿里巴巴按关键字搜索新品数据 API 使用说明
The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered
0627~ holiday knowledge summary
jmeter -- prometheus+grafana服务器性能可视化
【OpenCV】—阈值化
继承与派生
Mac database management software Navicat premium essentials mac
The solution of single chip microcomputer not supporting printf floating point type
Definition and storage of adjacency table and adjacency storage of directed graph and undirected graph
How to quickly upload files to Google Lab
0616项目二结束~~总总结
如何向 google colab 快速上传文件