1
from heapq import heapify, heappop, heappush5
def __init__(self, rating, food):9
def __lt__(self, other):10
if other.rating == self.rating:11
return self.food < other.food12
return self.rating < other.rating16
def __init__(self, foods: List[str], cuisines: List[str], ratings: List[int]):17
self.cuis_to_score_heap = defaultdict(list)18
self.food_to_latest_ratings = defaultdict(int)19
self.food_to_cuis = defaultdict(str)21
for food, cuis, rating in zip(foods, cuisines, ratings):22
self.food_to_cuis[food] = cuis23
self.food_to_latest_ratings[food] = rating24
heappush(self.cuis_to_score_heap[cuis], RatedFood(-rating, food))26
def changeRating(self, food: str, newRating: int) -> None:27
self.food_to_latest_ratings[food] = newRating28
cuis = self.food_to_cuis[food]29
heappush(self.cuis_to_score_heap[cuis], RatedFood(-newRating, food))31
def highestRated(self, cuisine: str) -> str:33
ratedFood = heappop(self.cuis_to_score_heap[cuisine])34
if self.food_to_latest_ratings[ratedFood.food] == (-ratedFood.rating):36
# because the food item is still valid, we put it back into the heap37
heappush(self.cuis_to_score_heap[cuisine], ratedFood)