728x90
https://leetcode.com/problems/design-a-food-rating-system/
class FoodRatings:
def __init__(self, foods: List[str], cuisines: List[str], ratings: List[int]):
self.cuisines_food = defaultdict(SortedSet) # cuisine -> (rating, food)
self.cuisines = {} # food -> cuisine
self.ratings = {} #food -> rating
for i in range(len(foods)):
self.cuisines_food[cuisines[i]].add((-ratings[i], foods[i]))
self.cuisines[foods[i]] = cuisines[i]
self.ratings[foods[i]] = ratings[i]
def changeRating(self, food: str, newRating: int) -> None:
c = self.cuisines[food]
r = self.ratings[food]
self.cuisines_food[c].remove((-r, food))
self.cuisines_food[c].add((-newRating, food))
self.ratings[food] = newRating
def highestRated(self, cuisine: str) -> str:
return self.cuisines_food[cuisine][0][1]
nothing special here, since we want highest rating we used SortedSet but with - on ratings do sort in descending order
728x90
'Algorithm > NeetCode' 카테고리의 다른 글
Minimum Number of Operations to Make Array Empty (0) | 2024.11.23 |
---|---|
Convert an Array Into a 2D Array With Conditions (0) | 2024.11.22 |
Sum of Absolute Differences in a Sorted Array (0) | 2024.11.20 |
Majority Element II (0) | 2024.11.19 |
Champagne Tower (0) | 2024.11.18 |