当前位置:网站首页>2022.07.24 (lc_6126_design food scoring system)
2022.07.24 (lc_6126_design food scoring system)
2022-07-25 12:38:00 【Leeli9316】

Method :Map+TreeSet
class FoodRatings {
// Use a food information class to represent the food name of food 、 The way of cooking 、 score
class FoodInfo {
String food;
String cuisine;
int rating;
FoodInfo(String food, String cuisine, int rating) {
this.food = food;
this.cuisine = cuisine;
this.rating = rating;
}
}
// The food name corresponds to a food information class
Map<String, FoodInfo> foodInfoMap = new HashMap<>();
// The cooking method corresponds to one TreeSet,TreeSet Store all this cooking , And customize the sorting according to the meaning of the topic
Map<String, TreeSet<FoodInfo>> cuisineMap = new HashMap<>();
public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
for (int i = 0; i < foods.length; i++) {
FoodInfo foodInfo = new FoodInfo(foods[i], cuisines[i], ratings[i]);
foodInfoMap.put(foods[i], foodInfo);
if (!cuisineMap.containsKey(cuisines[i])) {
cuisineMap.put(cuisines[i], new TreeSet<>(((o1, o2) -> {
// Sorting rules for foods cooked in the same way : First, arrange them in descending order ,
// If the score is the same , Then arrange the food names in ascending order according to the dictionary .
if (o1.rating != o2.rating) return o2.rating - o1.rating;
return o1.food.compareTo(o2.food);
})));
}
// In the way of cooking TreeSet Add food information to
cuisineMap.get(cuisines[i]).add(foodInfo);
}
}
public void changeRating(String food, int newRating) {
FoodInfo foodInfo = foodInfoMap.get(food);
// When changing the score, you need to put TreeSet Delete the scoring information in , Otherwise, it is wrong to change the position after scoring
// Delete first , Then change the score , Finally add TreeSet Find the right position
cuisineMap.get(foodInfo.cuisine).remove(foodInfo);
foodInfo.rating = newRating;
cuisineMap.get(foodInfo.cuisine).add(foodInfo);
}
public String highestRated(String cuisine) {
// Directly return to the corresponding cooking method TreeSet The first food name of
return cuisineMap.get(cuisine).first().food;
}
}Be careful :
- When updating the score Delete and then re insert , Trigger the rotation of nodes in the red black tree .
边栏推荐
- Pytorch environment configuration and basic knowledge
- Analysis of TCP packet capturing using Wireshark
- perf 性能调试
- Resttemplate and ribbon are easy to use
- PyTorch项目实战—FashionMNIST时装分类
- 水博士2
- Leetcode 0133. clone diagram
- 1.1.1 welcome to machine learning
- R language uses the ggarrange function of ggpubr package to combine multiple images, and uses the ggexport function to save the visual images in JPEG format (width parameter specifies width, height pa
- 搭建Vision Transformer系列实践,终于见面了,Timm库!
猜你喜欢

If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing

交换机链路聚合详解【华为eNSP】

Eureka usage record
![[fluent -- example] case 1: comprehensive example of basic components and layout components](/img/d5/2392d9cb8550aa2692c8b41303d507.png)
[fluent -- example] case 1: comprehensive example of basic components and layout components

scrapy 爬虫框架简介
![[micro service ~sentinel] sentinel degradation, current limiting, fusing](/img/60/448c5f40af4c0937814c243bd7cb04.png)
[micro service ~sentinel] sentinel degradation, current limiting, fusing

More accurate and efficient segmentation of organs-at-risk in radiotherapy with Convolutional Neural

Zuul gateway use
![[shutter -- layout] stacked layout (stack and positioned)](/img/01/c588f75313580063cf32cc01677600.jpg)
[shutter -- layout] stacked layout (stack and positioned)

Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?
随机推荐
1.1.1 welcome to machine learning
Pytorch visualization
状态(State)模式
Can't delete the blank page in word? How to operate?
Ansible
【四】布局视图和布局工具条使用
Table partition of MySQL
Numpy first acquaintance
Is the securities account opened by qiniu safe? How to open an account
The first scratch crawler
MySQL练习二
论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》
搭建Vision Transformer系列实践,终于见面了,Timm库!
使用TensorBoard可视化训练过程
[ROS advanced chapter] Lecture 9 programming optimization of URDF and use of xacro
Feign use
Software testing interview question: Please list the testing methods of several items?
公安部:国际社会普遍认为中国是世界上最安全的国家之一
2022.07.24(LC_6125_相等行列对)
Azure Devops(十四) 使用Azure的私有Nuget仓库