Advertisement
smj007

Untitled

Mar 7th, 2024
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. class FoodRatings:
  2.  
  3.     def __init__(self, foods: List[str], cuisines: List[str], ratings: List[int]):
  4.        
  5.         self.foodRating = {}
  6.         self.cuisineRatingFood = defaultdict(list)
  7.         self.cuisineFood = {}
  8.         for food, cuisine, rating in zip(foods, cuisines, ratings):
  9.             self.foodRating[food] = rating
  10.             heapq.heappush(self.cuisineRatingFood[cuisine], [-rating, food])
  11.             self.cuisineFood[food] = cuisine
  12.  
  13.     def changeRating(self, food: str, newRating: int) -> None:
  14.         self.foodRating[food] = newRating
  15.         cuisine = self.cuisineFood[food]
  16.         heapq.heappush(self.cuisineRatingFood[cuisine], [-newRating, food])
  17.  
  18.     def highestRated(self, cuisine: str) -> str:
  19.         heap = self.cuisineRatingFood[cuisine]
  20.  
  21.         while heap:
  22.             top, food = heapq.heappop(heap)
  23.             if -top == self.foodRating[food]:
  24.                 return food
  25.  
  26.         return ""
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement