# 2353. Design a Food Rating System

T:  FoodRatings() : O(nlogn), changeRating(): O(logn), highestRated: O(1)

S: O(n)

````java
```java
class FoodRatings {
    class Cuisine {
        String cuisine;
        int rating;
        Cuisine(String cuisine, int rating) {
            this.cuisine = cuisine;
            this.rating = rating;
        }
    }
    class Food {
        String food;
        int rating;
        Food(int rating, String food) {
            this.food = food;
            this.rating = rating;
        }
    }

    private Map<String, Cuisine> foodMap;
    private Map<String, TreeSet<Food>> sortedMap;
    public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
        foodMap = new HashMap<>();
        sortedMap = new HashMap<>();
        for (int i = 0; i < foods.length; i++) {
            foodMap.put(foods[i], new Cuisine(cuisines[i], ratings[i]));
            sortedMap.putIfAbsent(cuisines[i], new TreeSet<>((a, b) -> {
                int compareRating = b.rating - a.rating; // 由大到小
                if (compareRating == 0) { // same key
                    return a.food.compareTo(b.food);
                }
                return compareRating; // return large one
            }));
            sortedMap.get(cuisines[i]).add(new Food(ratings[i], foods[i]));
        }
    }
    
    public void changeRating(String food, int newRating) {
        Cuisine cuisine = foodMap.get(food);
        int oldRating = cuisine.rating;
        foodMap.get(food).rating = newRating;
        sortedMap.get(cuisine.cuisine).remove(new Food(oldRating, food));
        sortedMap.get(cuisine.cuisine).add(new Food(newRating, food));
    }
    
    public String highestRated(String cuisine) {
        Food first = sortedMap.get(cuisine).first(); // 由大到小, so 取 first
        if (first != null) {
            return first.food;
        }
        return "";
    }
}

/**
 * Your FoodRatings object will be instantiated and called as such:
 * FoodRatings obj = new FoodRatings(foods, cuisines, ratings);
 * obj.changeRating(food,newRating);
 * String param_2 = obj.highestRated(cuisine);

sortedMap:
 cuisine -> rating, food

 food, (cuisine,rating) 
 */
```
````


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/weekly-contest/2353.-design-a-food-rating-system.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
