leetcode-6126:设计食物评分系统

简介: leetcode-6126:设计食物评分系统

题目

题目连接

解题

方法一:有序map+有序set

根据烹饪方式,可以获得菜品和对应的得分,里面选择一个得分最大的。

1.首先使用unordered_map<string,XXXX> 根据烹饪方式,获取对应得菜品和得分

2.那么如何获得 得分 最大呢,可以使用有序map(红黑树),以得分作为key,从大到小排序。

因此有map<int,XXXX,greater<int>>,于是可以通过得分获得对应的菜品

3.为了保证菜品为字典序,因此使用有序集合,从小到大排序 ,set<string>

于是

unordered_map<string,map<int,set<string>,greater<int>>> map;

就可以根据 烹饪方式---->最大得分---->字典序最小的

class FoodRatings {
public:
    map<string,int> foodRating;//菜品--->得分
    map<string,string> foodClass;//菜品-->烹饪方式
    unordered_map<string,map<int,set<string>,greater<int>>> map;//烹饪方式---->得分(从大到小)---->菜品
    FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
        for(int i=0;i<foods.size();i++){
            foodRating[foods[i]]=ratings[i];
            foodClass[foods[i]]=cuisines[i];
            map[cuisines[i]][ratings[i]].insert(foods[i]);
        }
    }
    void changeRating(string food, int newRating) {
      //从集合中删除对应的旧菜品得分
        map[foodClass[food]][foodRating[food]].erase(food);
        if(map[foodClass[food]][foodRating[food]].empty()){//如果对应得分的map为空,那么就删除该map
            map[foodClass[food]].erase(foodRating[food]);
        }
        //记录新菜品得分
        foodRating[food]=newRating;
        map[foodClass[food]][newRating].insert(food);
    }
    string highestRated(string cuisine) {
        return *(map[cuisine].begin()->second.begin());
    }
};
相关文章
|
1月前
|
SQL
leetcode-SQL-1173. 即时食物配送 I
leetcode-SQL-1173. 即时食物配送 I
29 0
|
1月前
|
索引 容器
leetcode-6130:设计数字容器系统
leetcode-6130:设计数字容器系统
26 0
|
机器学习/深度学习 算法
【刷穿 LeetCode】282. 给表达式添加运算符 : 一道利用「代数系统」的回溯题
【刷穿 LeetCode】282. 给表达式添加运算符 : 一道利用「代数系统」的回溯题
|
数据库
LeetCode(数据库)- 报告系统状态的连续日期
LeetCode(数据库)- 报告系统状态的连续日期
90 0
|
存储 C++
LeetCode 2043. 简易银行系统
LeetCode 2043. 简易银行系统
199 0
[LeetCode] Find Duplicate File in System 在系统中寻找重复文件
Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of d.
1712 0
|
存储
[LeetCode] Design Log Storage System 设计日志存储系统
You are given several logs that each log contains a unique id and timestamp. Timestamp is a string that has the following format: Year:Month:Day:Hour:Minute:Second, for example, 2017:01:01:23:59:59.
1591 0
|
11天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-2
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
11天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-1
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题