当前位置:网站首页>2022.07.24(LC_6126_设计食物评分系统)
2022.07.24(LC_6126_设计食物评分系统)
2022-07-25 11:45:00 【Leeli9316】

方法:Map+TreeSet
class FoodRatings {
//用一个食物信息类表示食物的食物名、烹饪方式、评分
class FoodInfo {
String food;
String cuisine;
int rating;
FoodInfo(String food, String cuisine, int rating) {
this.food = food;
this.cuisine = cuisine;
this.rating = rating;
}
}
//食物名对应一个食物信息类
Map<String, FoodInfo> foodInfoMap = new HashMap<>();
//烹饪方式对应一个TreeSet,TreeSet存放所有这种烹饪方式的食物,并按题意自定义排序
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) -> {
//对烹饪方式相同食物的排序规则:首先按评分降序排列,
//如果评分相同,则按字典序对食物名字升序排列。
if (o1.rating != o2.rating) return o2.rating - o1.rating;
return o1.food.compareTo(o2.food);
})));
}
//在烹饪方式对应的TreeSet中添加食物信息
cuisineMap.get(cuisines[i]).add(foodInfo);
}
}
public void changeRating(String food, int newRating) {
FoodInfo foodInfo = foodInfoMap.get(food);
//改变评分时需要把TreeSet中的评分信息删掉,不然更改评分后的位置是错的
//即先删除,然后改评分,最后加入TreeSet找到正确位置
cuisineMap.get(foodInfo.cuisine).remove(foodInfo);
foodInfo.rating = newRating;
cuisineMap.get(foodInfo.cuisine).add(foodInfo);
}
public String highestRated(String cuisine) {
//直接返回烹饪方式对应的TreeSet的第一个食物名字
return cuisineMap.get(cuisine).first().food;
}
}注意:
- 更新评分时先删除再重新插入, 触发红黑树中节点的旋转。
边栏推荐
- R语言组间均值是否相同的成对比较:使用pairwise.t.test函数执行多个分组数据均值的两两成对假设检验
- How to access DMS database remotely? What is the IP address? What is the user name?
- Pytorch project practice - fashionmnist fashion classification
- R language uses LM function to build multiple linear regression model, step function to build forward stepwise regression model to screen the best subset of prediction variables, and scope parameter t
- NLP的基本概念1
- Pytorch environment configuration and basic knowledge
- Scott+scott law firm plans to file a class action against Yuga labs, or will confirm whether NFT is a securities product
- Behind the screen projection charge: iqiyi's quarterly profit, is Youku in a hurry?
- Brpc source code analysis (IV) -- bthread mechanism
- 【六】地图框设置
猜你喜欢

RestTemplate与Ribbon简单使用

Zero shot image retrieval (zero sample cross modal retrieval)

scrapy爬虫爬取动态网站

Feign使用

从云原生到智能化,深度解读行业首个「视频直播技术最佳实践图谱」

Client open download, welcome to try

Go garbage collector Guide

记录一次线上死锁的定位分析

WPF project introduction 1 - Design and development of simple login page

Resttemplate and ribbon are easy to use
随机推荐
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
Fault tolerant mechanism record
【高并发】通过源码深度分析线程池中Worker线程的执行流程
919. Complete binary tree inserter: simple BFS application problem
【九】坐标格网添加以及调整
容错机制记录
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置add参数在小提琴内部添加抖动数据点以及均值标准差竖线(jitter and mean_sd)
Brpc source code analysis (II) -- the processing process of brpc receiving requests
Median (二分答案 + 二分查找)
搭建Vision Transformer系列实践,终于见面了,Timm库!
Fiddler抓包APP
马斯克的“灵魂永生”:一半炒作,一半忽悠
scrapy爬虫爬取动态网站
记录一次线上死锁的定位分析
Scott+scott law firm plans to file a class action against Yuga labs, or will confirm whether NFT is a securities product
Pytorch main module
Introduction to the scratch crawler framework
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the dot strip chart, set the palette parameter to configure the color of data points at different levels,
Location analysis of recording an online deadlock
【9】 Coordinate grid addition and adjustment