| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.DesignFoodRatingSystem; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class DesignFoodRatingSystemDictionaryWithPriorityQueue : IDesignFoodRatingSystem |
| | | 16 | | { |
| | 1 | 17 | | private readonly Dictionary<string, PriorityQueue<FoodRating, FoodRating>> _cuisineToFoodRatingsDictionary = []; |
| | 1 | 18 | | private readonly Dictionary<string, string> _foodToCuisineDictionary = []; |
| | 1 | 19 | | private readonly Dictionary<string, int> _foodToRatingDictionary = []; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Time complexity - O(n log n), where n is the total number of foods |
| | | 23 | | /// Space complexity - O(n), where n is the total number of foods |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="foods"></param> |
| | | 26 | | /// <param name="cuisines"></param> |
| | | 27 | | /// <param name="ratings"></param> |
| | 1 | 28 | | public DesignFoodRatingSystemDictionaryWithPriorityQueue(string[] foods, string[] cuisines, int[] ratings) |
| | 1 | 29 | | { |
| | 1 | 30 | | var n = foods.Length; |
| | | 31 | | |
| | 14 | 32 | | for (var i = 0; i < n; i++) |
| | 6 | 33 | | { |
| | 6 | 34 | | var food = foods[i]; |
| | 6 | 35 | | var cuisine = cuisines[i]; |
| | 6 | 36 | | var rating = ratings[i]; |
| | | 37 | | |
| | 6 | 38 | | _foodToCuisineDictionary[food] = cuisine; |
| | 6 | 39 | | _foodToRatingDictionary[food] = rating; |
| | | 40 | | |
| | 6 | 41 | | if (!_cuisineToFoodRatingsDictionary.TryGetValue(cuisine, out var queue)) |
| | 3 | 42 | | { |
| | 3 | 43 | | queue = new PriorityQueue<FoodRating, FoodRating>(); |
| | | 44 | | |
| | 3 | 45 | | _cuisineToFoodRatingsDictionary[cuisine] = queue; |
| | 3 | 46 | | } |
| | | 47 | | |
| | 6 | 48 | | var foodRating = new FoodRating(food, rating); |
| | | 49 | | |
| | 6 | 50 | | queue.Enqueue(foodRating, foodRating); |
| | 6 | 51 | | } |
| | 1 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Time complexity - O(log k), where k is the number of foods in the cuisine |
| | | 56 | | /// Space complexity - O(n + m), where n is the initial number of foods and m is the number of rating changes |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="food"></param> |
| | | 59 | | /// <param name="newRating"></param> |
| | | 60 | | public void ChangeRating(string food, int newRating) |
| | 2 | 61 | | { |
| | 2 | 62 | | _foodToRatingDictionary[food] = newRating; |
| | | 63 | | |
| | 2 | 64 | | var cuisine = _foodToCuisineDictionary[food]; |
| | | 65 | | |
| | 2 | 66 | | var foodRating = new FoodRating(food, newRating); |
| | | 67 | | |
| | 2 | 68 | | _cuisineToFoodRatingsDictionary[cuisine].Enqueue(foodRating, foodRating); |
| | 2 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Time complexity - O(m log k), where m si the number of stale items dequeued and k is the number of foods in |
| | | 73 | | /// cuisine |
| | | 74 | | /// Space complexity - O(1) |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="cuisine"></param> |
| | | 77 | | /// <returns></returns> |
| | | 78 | | public string HighestRated(string cuisine) |
| | 4 | 79 | | { |
| | 4 | 80 | | var queue = _cuisineToFoodRatingsDictionary[cuisine]; |
| | | 81 | | |
| | 4 | 82 | | while (queue.Count > 0) |
| | 4 | 83 | | { |
| | 4 | 84 | | var foodRating = queue.Peek(); |
| | | 85 | | |
| | 4 | 86 | | var actualRating = _foodToRatingDictionary[foodRating.Food]; |
| | | 87 | | |
| | 4 | 88 | | if (foodRating.Rating == actualRating) |
| | 4 | 89 | | { |
| | 4 | 90 | | return foodRating.Food; |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | queue.Dequeue(); |
| | 0 | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return string.Empty; |
| | 4 | 97 | | } |
| | | 98 | | |
| | | 99 | | private readonly struct FoodRating : IComparable<FoodRating> |
| | | 100 | | { |
| | 10 | 101 | | public string Food { get; } |
| | | 102 | | |
| | 14 | 103 | | public int Rating { get; } |
| | | 104 | | |
| | | 105 | | public FoodRating(string food, int rating) |
| | 8 | 106 | | { |
| | 8 | 107 | | Food = food; |
| | 8 | 108 | | Rating = rating; |
| | 8 | 109 | | } |
| | | 110 | | |
| | | 111 | | public int CompareTo(FoodRating foodRating) |
| | 5 | 112 | | { |
| | 5 | 113 | | var ratingCompare = CompareToRating(foodRating.Rating); |
| | | 114 | | |
| | 5 | 115 | | if (ratingCompare == 0) |
| | 1 | 116 | | { |
| | 1 | 117 | | return CompareToFood(foodRating.Food); |
| | | 118 | | } |
| | | 119 | | |
| | 4 | 120 | | return ratingCompare; |
| | 5 | 121 | | } |
| | | 122 | | |
| | | 123 | | private int CompareToRating(int rating) |
| | 5 | 124 | | { |
| | 5 | 125 | | return rating.CompareTo(Rating); |
| | 5 | 126 | | } |
| | | 127 | | |
| | | 128 | | private int CompareToFood(string food) |
| | 1 | 129 | | { |
| | 1 | 130 | | return string.Compare(Food, food, StringComparison.OrdinalIgnoreCase); |
| | 1 | 131 | | } |
| | | 132 | | } |
| | | 133 | | } |