Skip to content

Commit

Permalink
Create 2353_Design_a_Food_Rating_System.java
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhuzaima committed Dec 17, 2023
1 parent 516c44a commit fcce68a
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions 2353_Design_a_Food_Rating_System.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// id: 2353
// Name: Design a Food Rating System
// link: https://leetcode.com/problems/design-a-food-rating-system/
// Difficulty: Medium

class FoodRatings {

class FoodItem implements Comparable<FoodItem> {
String food, cuisine;
int rating;
public FoodItem(String food, String cuisine, int rating) {
this.food = food;
this.cuisine = cuisine;
this.rating = rating;
}
public int compareTo(FoodItem other) {
if (other.rating != this.rating) {
return other.rating - this.rating; // reversed
}
return this.food.compareTo(other.food);
}
}

Map<String, FoodItem> map; // food name to item
Set<FoodItem> set; // sorted set

public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
map = new HashMap<>();
set = new TreeSet<>();
for (int i = 0; i < foods.length; i++) {
FoodItem fi = new FoodItem(foods[i], cuisines[i], ratings[i]);
map.put(foods[i], fi);
set.add(fi);
}
}

public void changeRating(String food, int newRating) {
FoodItem fi = map.get(food);
set.remove(fi); // reinsert in set to keep sorted
fi.rating = newRating;
set.add(fi);
}

public String highestRated(String cuisine) {
Iterator<FoodItem> it = set.iterator();

while (it.hasNext()) {
FoodItem fi = it.next();
if (fi.cuisine.equals(cuisine)) {
return fi.food;
}
}
return null;
}
}

0 comments on commit fcce68a

Please sign in to comment.