Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ee7f7ed

Browse files
D. J.:
- Added the leetcode problem and solution for 2353
1 parent adcfffd commit ee7f7ed

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@
291291
- [1964 Find the Longest Valid Obstacle Course at Each Position](https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/description/)
292292
- [2140 Solving Questions With Brainpower](https://leetcode.com/problems/solving-questions-with-brainpower/description/)
293293
- [2215 Find the Difference of Two Arrays](https://leetcode.com/problems/find-the-difference-of-two-arrays/description/)
294+
- [2353 Design a Food Rating System](https://leetcode.com/problems/design-a-food-rating-system/description/)
294295
- [2390 Removing Stars From a String](https://leetcode.com/problems/removing-stars-from-a-string/description/)
295296
- [2466 Count Ways To Build Good Strings](https://leetcode.com/problems/count-ways-to-build-good-strings/description/)
296297
- [2523 Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/description/)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import collections
2+
import heapq
3+
from dataclasses import dataclass, field
4+
from typing import List
5+
6+
7+
@dataclass(order=True)
8+
class Food:
9+
"""Item stored in the priority queue."""
10+
11+
rating: int = field(compare=True)
12+
food: str = field(compare=True)
13+
cuisine: str = field(compare=False)
14+
15+
16+
class FoodRatings:
17+
"""
18+
Design a food rating system that can do the following:
19+
- Modify the rating of a food item listed in the system.
20+
- Return the highest-rated food item for a type of cuisine in the system.
21+
22+
Implement the FoodRatings class:
23+
- FoodRatings(String[] foods, String[] cuisines, int[] ratings) Initializes the
24+
system. The food items are described by foods, cuisines and ratings, all of which
25+
have a length of n.
26+
- foods[i] is the name of the ith food,
27+
- cuisines[i] is the type of cuisine of the ith food, and
28+
- ratings[i] is the initial rating of the ith food.
29+
- void changeRating(String food, int newRating) Changes the rating of the food item
30+
with the name food.
31+
- String highestRated(String cuisine) Returns the name of the food item that has the
32+
highest rating for the given type of cuisine. If there is a tie, return the item
33+
with the lexicographically smaller name.
34+
35+
Note that a string x is lexicographically smaller than string y if x comes before y
36+
in dictionary order, that is, either x is a prefix of y, or if i is the first
37+
position such that x[i] != y[i], then x[i] comes before y[i] in alphabetic order.
38+
"""
39+
40+
def __init__(self, foods: List[str], cuisines: List[str], ratings: List[int]):
41+
self.food_to_cuisine = {}
42+
self.food_to_rating = {}
43+
self.cuisine_to_max_food = collections.defaultdict(list)
44+
45+
for f, c, r in zip(foods, cuisines, ratings, strict=False):
46+
self.food_to_cuisine[f] = c
47+
self.food_to_rating[f] = -r
48+
heapq.heappush(self.cuisine_to_max_food[c], Food(-r, f, c))
49+
50+
def changeRating(self, food: str, newRating: int) -> None:
51+
# Update food_to_rating
52+
self.food_to_rating[food] = -newRating
53+
54+
# Insert the new food element in the priority queue
55+
cuisine = self.food_to_cuisine[food]
56+
heapq.heappush(
57+
self.cuisine_to_max_food[cuisine], Food(-newRating, food, cuisine)
58+
)
59+
60+
def highestRated(self, cuisine: str) -> str:
61+
# Get the highest rated food of
62+
max_food = self.cuisine_to_max_food[cuisine][0]
63+
64+
while self.food_to_rating[max_food.food] != max_food.rating:
65+
heapq.heappop(self.cuisine_to_max_food[cuisine])
66+
max_food = self.cuisine_to_max_food[cuisine][0]
67+
68+
return max_food.food
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from awesome_python_leetcode._2353_design_a_food_rating_system import FoodRatings
2+
3+
4+
def test_func():
5+
"""Tests the solution of a LeetCode problem."""
6+
food_ratings = FoodRatings(
7+
foods=["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"],
8+
cuisines=["korean", "japanese", "japanese", "greek", "japanese", "korean"],
9+
ratings=[9, 12, 8, 15, 14, 7],
10+
)
11+
assert food_ratings.highestRated("korean") == "kimchi"
12+
assert food_ratings.highestRated("japanese") == "ramen"
13+
14+
food_ratings.changeRating("sushi", 16)
15+
assert food_ratings.highestRated("japanese") == "sushi"
16+
17+
food_ratings.changeRating("ramen", 16)
18+
assert food_ratings.highestRated("japanese") == "ramen"

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /