forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign-a-food-rating-system.py
45 lines (39 loc) · 1.37 KB
/
design-a-food-rating-system.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
# Time: ctor: O(nlogn)
# changeRating: O(logn)
# highestRated: O(1)
# Space: O(n)
import collections
import itertools
from sortedcontainers import SortedList
# sorted list
class FoodRatings(object):
def __init__(self, foods, cuisines, ratings):
"""
:type foods: List[str]
:type cuisines: List[str]
:type ratings: List[int]
"""
self.__food_to_cuisine = {}
self.__food_to_rating = {}
self.__cusine_to_rating_foods = collections.defaultdict(SortedList)
for food, cuisine, rating in itertools.izip(foods, cuisines, ratings):
self.__food_to_cuisine[food] = cuisine
self.__food_to_rating[food] = rating
self.__cusine_to_rating_foods[cuisine].add((-rating, food))
def changeRating(self, food, newRating):
"""
:type food: str
:type newRating: int
:rtype: None
"""
old_rating = self.__food_to_rating[food]
cuisine = self.__food_to_cuisine[food]
self.__cusine_to_rating_foods[cuisine].remove((-old_rating, food))
self.__food_to_rating[food] = newRating
self.__cusine_to_rating_foods[cuisine].add((-newRating, food))
def highestRated(self, cuisine):
"""
:type cuisine: str
:rtype: str
"""
return self.__cusine_to_rating_foods[cuisine][0][1]