aboutsummaryrefslogtreecommitdiff
path: root/design_food_rating_sys.py
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /design_food_rating_sys.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'design_food_rating_sys.py')
-rw-r--r--design_food_rating_sys.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/design_food_rating_sys.py b/design_food_rating_sys.py
new file mode 100644
index 0000000..4c9268c
--- /dev/null
+++ b/design_food_rating_sys.py
@@ -0,0 +1,50 @@
+# 2353. Design a Food Rating System
+from sortedcontainers import SortedSet
+
+"""
+design a food rating system that can do the following:
+- modify the rating of a food item listed in the system
+- return the highest rated food item for a type of cuisine in the system
+implement the 'FoodRatings' class. note that a string 'x' is
+lexicographically smaller than string 'y' if 'x' comes before 'y' in
+dictionary order, that is, either 'x' is a prefix of 'y' or if 'i' is the
+first position such that 'x[i] != y[i], then x[i] comes beofre y[i] in
+alpabetic order
+"""
+
+
+class FoodRatings(object):
+ def __init__(self, foods, cuisines, ratings):
+ """
+ :type foods: List[str]
+ :type cuisines: List[str]
+ :type ratings: List[int]
+ """
+ self.food_rating_map = {}
+ self.food_cuisine_map = {}
+ self.cuisine_food_map = defaultdict(SortedSet)
+
+ for i in range(len(foods)):
+ self.food_rating_map[foods[i]] = ratings[i]
+ self.food_cuisine_map[foods[i]] = cuisines[i]
+ self.cuisine_food_map[cuisines[i]].add((-ratings[i], foods[i]))
+
+ def changeRating(self, food, newRating):
+ """
+ :type food: str
+ :type newRating: int
+ :rtype: None
+ """
+ cuisine_name = self.food_cuisine_map[food]
+ old_element = (-self.food_rating_map[food], food)
+ self.cuisine_food_map[cuisine_name].remove(old_element)
+ self.food_rating_map[food] = newRating
+ self.cuisine_food_map[cuisine_name].add((-newRating, food))
+
+ def highestRated(self, cuisine):
+ """
+ :type cuisine: str
+ :rtype: str
+ """
+ highest_rated = self.cuisine_food_map[cuisine][0]
+ return highest_rated[1]