当前位置:网站首页>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 .
边栏推荐
- 919. Complete binary tree inserter: simple BFS application problem
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
- “蔚来杯“2022牛客暑期多校训练营2 补题题解(G、J、K、L)
- cmake 学习使用笔记(二)库的生成与使用
- 什么是CI/CD?
- 请问一下,使用数据集成从postgreSQL导数据到Mysql数据库,有部分数据的字段中出现emoj
- 力扣 83双周赛T4 6131.不可能得到的最短骰子序列、303 周赛T4 6127.优质数对的数目
- Pytorch project practice - fashionmnist fashion classification
- Synergetic process
- PyTorch主要模块
猜你喜欢

Analysis of TCP packet capturing using Wireshark
![[ROS advanced chapter] Lecture 9 programming optimization of URDF and use of xacro](/img/a2/9b676d0f1b33cc7d413cee6c52d76d.png)
[ROS advanced chapter] Lecture 9 programming optimization of URDF and use of xacro

scrapy 爬虫框架简介

Eureka registration center opens password authentication - record

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

Technical management essay

3.2.1 什么是机器学习?

Interviewer: "classmate, have you ever done a real landing project?"

LeetCode 1184. 公交站间的距离

Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?
随机推荐
软件测试流程包括哪些内容?测试方法有哪些?
Ansible
Introduction to the scratch crawler framework
Basic concepts of NLP 1
想要白嫖正则大全是吧?这一次给你个够!
Detailed explanation of flex box
防范SYN洪泛攻击的方法 -- SYN cookie
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
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
SSTI 模板注入漏洞总结之[BJDCTF2020]Cookie is so stable
PyTorch主要模块
Maskgae: masked graph modeling meets graph autoencoders
阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?
【8】 Clever use of color finder
mysql实现一张表数据插入另一张表
Jenkins configuration pipeline
【Flutter -- 布局】层叠布局(Stack和Positioned)
【3】 DEM mountain shadow effect
【七】图层显示和标注
2022河南萌新联赛第(三)场:河南大学 I - 旅行