Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Leetcode 2353. Design a Food Rating System #132

Open
Woodyiiiiiii opened this issue Jul 24, 2022 · 0 comments
Open

Leetcode 2353. Design a Food Rating System #132

Woodyiiiiiii opened this issue Jul 24, 2022 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

  1. 集合成一个父类Food
  2. 用PriorityQueue排序,否则超时
  3. 使用Map互相寻找对应关系
class FoodRatings {

    HashMap<String, PriorityQueue<Food>> x = new HashMap<>(); // get pq from cuisine name
    
    HashMap<String, Food> menu = new HashMap<>(); // get Food (object) by food name

    public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
        for(int i=0; i<foods.length; i++){
            if(!x.containsKey(cuisines[i])){
                PriorityQueue<Food> pq = new PriorityQueue<>((a,b)->
                b.rating-a.rating==0 ? a.name.compareTo(b.name) : b.rating-a.rating);
                x.put(cuisines[i], pq);
            }

            Food curr = new Food(foods[i], cuisines[i], ratings[i]);
            PriorityQueue<Food> pq = x.get(cuisines[i]);
            pq.add(curr);
            menu.put(foods[i], curr);
        }
    }

    public void changeRating(String food, int newRating) {
        Food curr = menu.get(food);
        PriorityQueue<Food> pq = x.get(curr.cuisine);
        pq.remove(curr);
        curr.rating = newRating;
        pq.add(curr);
    }

    public String highestRated(String cuisine) {
        return x.get(cuisine).peek().name;
    }
    
}

class Food{
    int rating;
    String name, cuisine;
    Food(String name, String cuisine, int rating){
        this.name = name; this.rating = rating; this.cuisine = cuisine;
    }
}

/**
 * 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);
 */

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant