当前位置:网站首页>6126. 设计食物评分系统
6126. 设计食物评分系统
2022-07-24 18:05:00 【hys__handsome】
设计一个支持下述操作的食物评分系统:
- 修改 系统中列出的某种食物的评分。
- 返回系统中某一类烹饪方式下评分最高的食物。
实现 FoodRatings 类:
FoodRatings(String[] foods, String[] cuisines, int[] ratings)初始化系统。食物由foods、cuisines和ratings描述,长度均为n。foods[i]是第i种食物的名字。cuisines[i]是第i种食物的烹饪方式。ratings[i]是第i种食物的最初评分。
void changeRating(String food, int newRating)修改名字为food的食物的评分。String highestRated(String cuisine)返回指定烹饪方式cuisine下评分最高的食物的名字。如果存在并列,返回 字典序较小 的名字。
注意,字符串 x 的字典序比字符串 y 更小的前提是:x 在字典中出现的位置在 y 之前,也就是说,要么 x 是 y 的前缀,或者在满足 x[i] != y[i] 的第一个位置 i 处,x[i] 在字母表中出现的位置在 y[i] 之前。
示例:
输入
["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"]]
输出
[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
解释
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"); // 返回 "kimchi"
// "kimchi" 是分数最高的韩式料理,评分为 9 。
foodRatings.highestRated("japanese"); // 返回 "ramen"
// "ramen" 是分数最高的日式料理,评分为 14 。
foodRatings.changeRating("sushi", 16); // "sushi" 现在评分变更为 16 。
foodRatings.highestRated("japanese"); // 返回 "sushi"
// "sushi" 是分数最高的日式料理,评分为 16 。
foodRatings.changeRating("ramen", 16); // "ramen" 现在评分变更为 16 。
foodRatings.highestRated("japanese"); // 返回 "ramen"
// "sushi" 和 "ramen" 的评分都是 16 。
// 但是,"ramen" 的字典序比 "sushi" 更小。
提示:
1 <= n <= 2 * 10^4n == foods.length == cuisines.length == ratings.length1 <= foods[i].length, cuisines[i].length <= 10foods[i]、cuisines[i]由小写英文字母组成1 <= ratings[i] <= 10^8foods中的所有字符串 互不相同- 在对
changeRating的所有调用中,food是系统中食物的名字。 - 在对
highestRated的所有调用中,cuisine是系统中 至少一种 食物的烹饪方式。 - 最多调用
changeRating和highestRated总计2 * 10^4次
通过set的
时间快速删除、插入、维护有序。不然每次在highestRated方法上都使用
查找会超时,而set只需要取第一个即可。
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) { //主要优化在这
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;
}
};
边栏推荐
- Section 11 cache avalanche, hot data failure follow Daewoo redis ------- directory post
- Model saving and loading of sklearn
- C语言自定义类型 — 枚举
- 文件上传漏洞——.user.ini与.htaccess
- com.mysql.cj.jdbc.exceptions. MySQLTransactionRollbackException: Deadlock found when trying to get lo
- 05mysql lock analysis
- Learn redisson from scratch ------- topics (subscription and distribution)
- Laravel笔记-用户登录时密码进行RSA加密(提高系统安全性)
- Definition and storage of adjacency table and adjacency storage of directed graph and undirected graph
- Int8 & int8, have you ever stumbled like this?
猜你喜欢

Brats18 - Multimodal MR image brain tumor segmentation challenge continued

SV casts and constants

C语言自定义类型讲解 — 结构体

C language programming training topics: K characters in left-handed string, little Lele and Euclidean, printing arrow pattern, civil servant interview, poplar matrix

Laravel笔记-用户登录时密码进行RSA加密(提高系统安全性)

Review and analysis of noodle dishes

【obs】依赖库: x264 vs 构建

分家后印象笔记过日子依然不好过,骚操作却不少

0630~ professional quality course

Three ways of redis cluster
随机推荐
0615~用自定义注解实现RBAC权限管理
How to quickly upload files to Google Lab
干货|值得收藏的三款子域名收集工具
Common methods of number and math classes
安装JumpServer
0701~ holiday summary
Section 11 cache avalanche, hot data failure follow Daewoo redis ------- directory post
Still building projects from scratch? This upgraded rapid development scaffold is worth a try!
700. 二叉搜索树中的搜索-dfs法
Tensorflow introductory tutorial (40) -- acunet
redis集群的三种方式
Shardingsphere database read / write separation
Common questions of testers during interview
Emerging potential of interactive virtual reality technology in drug discovery
如何遵循“低耦合”设计原则?
1688/ Alibaba searches new product data by keyword API instructions
C language custom type explanation - Consortium
Handwritten blog platform ~ the next day
分家后印象笔记过日子依然不好过,骚操作却不少
05mysql lock analysis