< 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
98%
Covered lines: 59
Uncovered lines: 1
Coverable lines: 60
Total lines: 133
Line coverage: 98.3%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
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(...)75%4492.3%
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{
 517    private readonly Dictionary<string, PriorityQueue<FoodRating, FoodRating>> _cuisineToFoodRatingsDictionary = [];
 518    private readonly Dictionary<string, string> _foodToCuisineDictionary = [];
 519    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>
 528    public DesignFoodRatingSystemDictionaryWithPriorityQueue(string[] foods, string[] cuisines, int[] ratings)
 529    {
 530        var n = foods.Length;
 31
 4032        for (var i = 0; i < n; i++)
 1533        {
 1534            var food = foods[i];
 1535            var cuisine = cuisines[i];
 1536            var rating = ratings[i];
 37
 1538            _foodToCuisineDictionary[food] = cuisine;
 1539            _foodToRatingDictionary[food] = rating;
 40
 1541            if (!_cuisineToFoodRatingsDictionary.TryGetValue(cuisine, out var queue))
 942            {
 943                queue = new PriorityQueue<FoodRating, FoodRating>();
 44
 945                _cuisineToFoodRatingsDictionary[cuisine] = queue;
 946            }
 47
 1548            var foodRating = new FoodRating(food, rating);
 49
 1550            queue.Enqueue(foodRating, foodRating);
 1551        }
 552    }
 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)
 561    {
 562        _foodToRatingDictionary[food] = newRating;
 63
 564        var cuisine = _foodToCuisineDictionary[food];
 65
 566        var foodRating = new FoodRating(food, newRating);
 67
 568        _cuisineToFoodRatingsDictionary[cuisine].Enqueue(foodRating, foodRating);
 569    }
 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)
 1479    {
 1480        var queue = _cuisineToFoodRatingsDictionary[cuisine];
 81
 1682        while (queue.Count > 0)
 1683        {
 1684            var foodRating = queue.Peek();
 85
 1686            var actualRating = _foodToRatingDictionary[foodRating.Food];
 87
 1688            if (foodRating.Rating == actualRating)
 1489            {
 1490                return foodRating.Food;
 91            }
 92
 293            queue.Dequeue();
 294        }
 95
 096        return string.Empty;
 1497    }
 98
 99    private readonly struct FoodRating : IComparable<FoodRating>
 100    {
 34101        public string Food { get; }
 102
 42103        public int Rating { get; }
 104
 105        public FoodRating(string food, int rating)
 20106        {
 20107            Food = food;
 20108            Rating = rating;
 20109        }
 110
 111        public int CompareTo(FoodRating foodRating)
 13112        {
 13113            var ratingCompare = CompareToRating(foodRating.Rating);
 114
 13115            if (ratingCompare == 0)
 2116            {
 2117                return CompareToFood(foodRating.Food);
 118            }
 119
 11120            return ratingCompare;
 13121        }
 122
 123        private int CompareToRating(int rating)
 13124        {
 13125            return rating.CompareTo(Rating);
 13126        }
 127
 128        private int CompareToFood(string food)
 2129        {
 2130            return string.Compare(Food, food, StringComparison.OrdinalIgnoreCase);
 2131        }
 132    }
 133}