-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2353_Design_a_Food_Rating_System.java
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|