-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2353.py
More file actions
41 lines (32 loc) · 1.33 KB
/
2353.py
File metadata and controls
41 lines (32 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import heapq
from collections import defaultdict
class FoodRatings:
def __init__(self, foods: List[str], cuisines: List[str], ratings: List[int]):
self.info = {}
self.cuisimp = defaultdict(list)
for foo, cuisi, rate in zip (foods, cuisines, ratings):
self.info[foo] = (cuisi, rate)
heapq.heappush(self.cuisimp[cuisi], (-rate, foo))
def changeRating(self, food: str, newRating: int) -> None:
cuisi, oldrate = self.info[food]
self.info[food] = (cuisi, newRating)
heapq.heappush(self.cuisimp[cuisi], (-newRating,food))
def highestRated(self, cuisine: str) -> str:
heap = self.cuisimp[cuisine]
while heap:
negrate, foodname = heap[0]
expectrate = - negrate
realcusi, realrate = self.info[foodname]
if expectrate == realrate:
return foodname
else:
heapq.heappop(heap)
return ""
# Your FoodRatings object will be instantiated and called as such:
# obj = FoodRatings(foods, cuisines, ratings)
# obj.changeRating(food,newRating)
# param_2 = obj.highestRated(cuisine)
# Your FoodRatings object will be instantiated and called as such:
# obj = FoodRatings(foods, cuisines, ratings)
# obj.changeRating(food,newRating)
# param_2 = obj.highestRated(cuisine)