2353. Design a Food Rating System #2184
-
Topics: Design a food rating system that can do the following:
Implement the
Note that a string Example 1:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to design a food rating system that supports modifying the rating of a food item and retrieving the highest-rated food item for a specific cuisine. The solution involves efficiently storing and updating food ratings while ensuring quick access to the highest-rated food for any given cuisine, considering lexicographical order in case of ties. Approach
Let's implement this solution in PHP: 2353. Design a Food Rating System <?php
class FoodRatings {
private $foodInfo;
private $cuisineHeaps;
/**
* @param String[] $foods
* @param String[] $cuisines
* @param Integer[] $ratings
*/
function __construct($foods, $cuisines, $ratings) {
$this->foodInfo = [];
$this->cuisineHeaps = [];
$n = count($foods);
for ($i = 0; $i < $n; $i++) {
$food = $foods[$i];
$cuisine = $cuisines[$i];
$rating = $ratings[$i];
$this->foodInfo[$food] = [$cuisine, $rating];
if (!isset($this->cuisineHeaps[$cuisine])) {
$this->cuisineHeaps[$cuisine] = new MyMaxHeap();
}
$this->cuisineHeaps[$cuisine]->insert([$rating, $food]);
}
}
/**
* @param String $food
* @param Integer $newRating
* @return NULL
*/
function changeRating($food, $newRating) {
list($cuisine, $oldRating) = $this->foodInfo[$food];
$this->foodInfo[$food] = [$cuisine, $newRating];
$this->cuisineHeaps[$cuisine]->insert([$newRating, $food]);
}
/**
* @param String $cuisine
* @return String
*/
function highestRated($cuisine) {
$heap = $this->cuisineHeaps[$cuisine];
while (true) {
$top = $heap->top();
list($rating, $food) = $top;
if ($this->foodInfo[$food][1] == $rating) {
return $food;
}
$heap->extract();
}
}
}
class MyMaxHeap extends SplMaxHeap {
/**
* @param $a
* @param $b
* @return int
*/
public function compare($a, $b): int {
if ($a[0] != $b[0]) {
return $a[0] - $b[0];
}
return strcmp($b[1], $a[1]);
}
}
/**
* Your FoodRatings object will be instantiated and called as such:
* $obj = FoodRatings($foods, $cuisines, $ratings);
* $obj->changeRating($food, $newRating);
* $ret_2 = $obj->highestRated($cuisine);
*/
// Test cases
$foods = ["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"];
$cuisines = ["korean", "japanese", "japanese", "greek", "japanese", "korean"];
$ratings = [9, 12, 8, 15, 14, 7];
$obj = new FoodRatings($foods, $cuisines, $ratings);
echo $obj->highestRated("korean") . PHP_EOL; // kimchi
echo $obj->highestRated("japanese") . PHP_EOL; // ramen
$obj->changeRating("sushi", 16);
echo $obj->highestRated("japanese") . PHP_EOL; // sushi
$obj->changeRating("ramen", 16);
echo $obj->highestRated("japanese") . PHP_EOL; // ramen
?> Explanation:
This approach efficiently manages dynamic updates and queries by leveraging hash maps and heaps, ensuring optimal performance for both operations. The lazy deletion strategy minimizes the overhead of updating existing heap entries, making the solution both practical and efficient. |
Beta Was this translation helpful? Give feedback.
We need to design a food rating system that supports modifying the rating of a food item and retrieving the highest-rated food item for a specific cuisine. The solution involves efficiently storing and updating food ratings while ensuring quick access to the highest-rated food for any given cuisine, considering lexicographical order in case of ties.
Approach
Data Structures:
foodInfo
: A hash map that stores each food's cuisine and current rating.cuisineHeaps
: A hash map where each key is a cuisine and the value is a max-heap (priority queue) that contains pairs of ratings and food names. The heap is ordered such that higher ratings come first, and in case of ties, lexicographically sm…