< Summary

Information
Class: LeetCode.Algorithms.DesignFoodRatingSystem.DesignFoodRatingSystemDictionaryWithPriorityQueue
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignFoodRatingSystem\DesignFoodRatingSystemDictionaryWithPriorityQueue.cs
Line coverage
95%
Covered lines: 57
Uncovered lines: 3
Coverable lines: 60
Total lines: 133
Line coverage: 95%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%44100%
ChangeRating(...)100%11100%
HighestRated(...)50%4476.92%
get_Food()100%11100%
get_Rating()100%11100%
.ctor(...)100%11100%
CompareTo(...)100%22100%
CompareToRating(...)100%11100%
CompareToFood(...)100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignFoodRatingSystem\DesignFoodRatingSystemDictionaryWithPriorityQueue.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.DesignFoodRatingSystem;
 13
 14/// <inheritdoc />
 15public sealed class DesignFoodRatingSystemDictionaryWithPriorityQueue : IDesignFoodRatingSystem
 16{
 117    private readonly Dictionary<string, PriorityQueue<FoodRating, FoodRating>> _cuisineToFoodRatingsDictionary = [];
 118    private readonly Dictionary<string, string> _foodToCuisineDictionary = [];
 119    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>
 128    public DesignFoodRatingSystemDictionaryWithPriorityQueue(string[] foods, string[] cuisines, int[] ratings)
 129    {
 130        var n = foods.Length;
 31
 1432        for (var i = 0; i < n; i++)
 633        {
 634            var food = foods[i];
 635            var cuisine = cuisines[i];
 636            var rating = ratings[i];
 37
 638            _foodToCuisineDictionary[food] = cuisine;
 639            _foodToRatingDictionary[food] = rating;
 40
 641            if (!_cuisineToFoodRatingsDictionary.TryGetValue(cuisine, out var queue))
 342            {
 343                queue = new PriorityQueue<FoodRating, FoodRating>();
 44
 345                _cuisineToFoodRatingsDictionary[cuisine] = queue;
 346            }
 47
 648            var foodRating = new FoodRating(food, rating);
 49
 650            queue.Enqueue(foodRating, foodRating);
 651        }
 152    }
 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)
 261    {
 262        _foodToRatingDictionary[food] = newRating;
 63
 264        var cuisine = _foodToCuisineDictionary[food];
 65
 266        var foodRating = new FoodRating(food, newRating);
 67
 268        _cuisineToFoodRatingsDictionary[cuisine].Enqueue(foodRating, foodRating);
 269    }
 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)
 479    {
 480        var queue = _cuisineToFoodRatingsDictionary[cuisine];
 81
 482        while (queue.Count > 0)
 483        {
 484            var foodRating = queue.Peek();
 85
 486            var actualRating = _foodToRatingDictionary[foodRating.Food];
 87
 488            if (foodRating.Rating == actualRating)
 489            {
 490                return foodRating.Food;
 91            }
 92
 093            queue.Dequeue();
 094        }
 95
 096        return string.Empty;
 497    }
 98
 99    private readonly struct FoodRating : IComparable<FoodRating>
 100    {
 10101        public string Food { get; }
 102
 14103        public int Rating { get; }
 104
 105        public FoodRating(string food, int rating)
 8106        {
 8107            Food = food;
 8108            Rating = rating;
 8109        }
 110
 111        public int CompareTo(FoodRating foodRating)
 5112        {
 5113            var ratingCompare = CompareToRating(foodRating.Rating);
 114
 5115            if (ratingCompare == 0)
 1116            {
 1117                return CompareToFood(foodRating.Food);
 118            }
 119
 4120            return ratingCompare;
 5121        }
 122
 123        private int CompareToRating(int rating)
 5124        {
 5125            return rating.CompareTo(Rating);
 5126        }
 127
 128        private int CompareToFood(string food)
 1129        {
 1130            return string.Compare(Food, food, StringComparison.OrdinalIgnoreCase);
 1131        }
 132    }
 133}