LeetCode 2353: Design a Food Rating System Solution

Master LeetCode problem 2353 (Design a Food Rating System), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation:

To solve this problem, we can use a HashMap to store the food items grouped by cuisine. For each cuisine, we can maintain a TreeMap to keep track of the highest-rated food item. When changing the rating of a food item, we update both the rating in the HashMap and the TreeMap. To find the highest-rated food item for a given cuisine, we can simply query the corresponding TreeMap.

Algorithm:

  1. Initialize a HashMap to store the food items grouped by cuisine and a TreeMap for each cuisine to store the highest-rated food item.
  2. In the constructor FoodRatings, populate the HashMap and TreeMap with the provided foods, cuisines, and ratings.
  3. Implement the changeRating method to update the rating of a food item in both the HashMap and the corresponding TreeMap.
  4. Implement the highestRated method to return the highest-rated food item for a given cuisine by querying the TreeMap.

Time Complexity:

  • Constructor: O(n) where n is the number of food items
  • changeRating: O(log n) for updating TreeMap
  • highestRated: O(log n) for querying TreeMap

Space Complexity:

  • O(n) for storing food items in the HashMap and TreeMap

:

Solution Code

import java.util.*;

class FoodRatings {
    Map<String, Map<String, Integer>> foodsByCuisine;
    Map<String, TreeMap<Integer, String>> highestRatedByCuisine;

    public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
        foodsByCuisine = new HashMap<>();
        highestRatedByCuisine = new HashMap<>();
        
        for (int i = 0; i < foods.length; i++) {
            foodsByCuisine.putIfAbsent(cuisines[i], new HashMap<>());
            highestRatedByCuisine.putIfAbsent(cuisines[i], new TreeMap<>(Collections.reverseOrder()));
            foodsByCuisine.get(cuisines[i]).put(foods[i], ratings[i]);
            highestRatedByCuisine.get(cuisines[i]).put(ratings[i], foods[i]);
        }
    }

    public void changeRating(String food, int newRating) {
        for (Map<String, Integer> map : foodsByCuisine.values()) {
            if (map.containsKey(food)) {
                String cuisine = map.keySet().stream().findFirst().get();
                int oldRating = map.get(food);
                map.put(food, newRating);
                highestRatedByCuisine.get(cuisine).remove(oldRating);
                highestRatedByCuisine.get(cuisine).put(newRating, food);
                break;
            }
        }
    }

    public String highestRated(String cuisine) {
        return highestRatedByCuisine.get(cuisine).firstEntry().getValue();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2353 (Design a Food Rating System)?

This page provides optimized solutions for LeetCode problem 2353 (Design a Food Rating System) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2353 (Design a Food Rating System)?

The time complexity for LeetCode 2353 (Design a Food Rating System) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2353 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2353 in Java, C++, or Python.

Back to LeetCode Solutions