-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode-2023-12-17.py
48 lines (43 loc) · 1.53 KB
/
leetcode-2023-12-17.py
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
42
43
44
45
46
47
48
"""
Question Link: https://leetcode.com/problems/design-a-food-rating-system/description/?envType=daily-question&envId=2023-12-17
"""
import heapq
from collections import defaultdict
class FoodRatings(object):
def __init__(self, foods, cuisines, ratings):
"""
:type foods: List[str]
:type cuisines: List[str]
:type ratings: List[int]
"""
self.cuisine_to_heap = defaultdict(list)
self.food_to_cuisine = {}
self.food_to_rating = defaultdict(int)
for i in range(len(foods)):
self.food_to_cuisine[foods[i]] = cuisines[i]
heapq.heappush(self.cuisine_to_heap[cuisines[i]], (-ratings[i], foods[i]))
self.food_to_rating[foods[i]] = -ratings[i]
def changeRating(self, food, newRating):
"""
:type food: str
:type newRating: int
:rtype: None
"""
cuisine = self.food_to_cuisine[food]
heapq.heappush(self.cuisine_to_heap[cuisine], (-newRating, food))
self.food_to_rating[food] = -newRating
def highestRated(self, cuisine):
"""
:type cuisine: str
:rtype: str
"""
smallest_lexico = None
while len(self.cuisine_to_heap[cuisine]) > 0:
curr = self.cuisine_to_heap[cuisine][0]
if curr[0] != self.food_to_rating[curr[1]]:
# delete old data
heapq.heappop(self.cuisine_to_heap[cuisine])
continue
smallest_lexico = curr[1]
break
return smallest_lexico