comments | difficulty | edit_url | rating | source | tags | ||||
---|---|---|---|---|---|---|---|---|---|
true |
中等 |
1781 |
第 303 场周赛 Q3 |
|
设计一个支持下述操作的食物评分系统:
- 修改 系统中列出的某种食物的评分。
- 返回系统中某一类烹饪方式下评分最高的食物。
实现 FoodRatings
类:
FoodRatings(String[] foods, String[] cuisines, int[] ratings)
初始化系统。食物由foods
、cuisines
和ratings
描述,长度均为n
。<ul> <li><code>foods[i]</code> 是第 <code>i</code> 种食物的名字。</li> <li><code>cuisines[i]</code> 是第 <code>i</code> 种食物的烹饪方式。</li> <li><code>ratings[i]</code> 是第 <code>i</code> 种食物的最初评分。</li> </ul> </li> <li><code>void changeRating(String food, int newRating)</code> 修改名字为 <code>food</code> 的食物的评分。</li> <li><code>String highestRated(String cuisine)</code> 返回指定烹饪方式 <code>cuisine</code> 下评分最高的食物的名字。如果存在并列,返回 <strong>字典序较小</strong> 的名字。</li>
注意,字符串 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 * 104
n == foods.length == cuisines.length == ratings.length
1 <= foods[i].length, cuisines[i].length <= 10
foods[i]
、cuisines[i]
由小写英文字母组成1 <= ratings[i] <= 108
foods
中的所有字符串 互不相同- 在对
changeRating
的所有调用中,food
是系统中食物的名字。 - 在对
highestRated
的所有调用中,cuisine
是系统中 至少一种 食物的烹饪方式。 - 最多调用
changeRating
和highestRated
总计2 * 104
次
from sortedcontainers import SortedSet
class FoodRatings:
def __init__(self, foods: List[str], cuisines: List[str], ratings: List[int]):
self.mp = {}
self.t = defaultdict(lambda: SortedSet(key=lambda x: (-x[0], x[1])))
for a, b, c in zip(foods, cuisines, ratings):
self.mp[a] = (b, c)
self.t[b].add((c, a))
def changeRating(self, food: str, newRating: int) -> None:
b, c = self.mp[food]
self.mp[food] = (b, newRating)
self.t[b].remove((c, food))
self.t[b].add((newRating, food))
def highestRated(self, cuisine: str) -> str:
return self.t[cuisine][0][1]
# Your FoodRatings object will be instantiated and called as such:
# obj = FoodRatings(foods, cuisines, ratings)
# obj.changeRating(food,newRating)
# param_2 = obj.highestRated(cuisine)
using pis = pair<int, string>;
class FoodRatings {
map<string, pis> mp;
map<string, set<pis>> t;
public:
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
int n = foods.size();
for (int i = 0; i < n; ++i) {
string a = foods[i], b = cuisines[i];
int c = ratings[i];
mp[a] = pis(c, b);
t[b].insert(pis(-c, a));
}
}
void changeRating(string food, int newRating) {
pis& p = mp[food];
t[p.second].erase(pis(-p.first, food));
p.first = newRating;
t[p.second].insert(pis(-p.first, food));
}
string highestRated(string cuisine) {
return t[cuisine].begin()->second;
}
};
/**
* Your FoodRatings object will be instantiated and called as such:
* FoodRatings* obj = new FoodRatings(foods, cuisines, ratings);
* obj->changeRating(food,newRating);
* string param_2 = obj->highestRated(cuisine);
*/