-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet-code-q-25.java
More file actions
67 lines (63 loc) · 2.52 KB
/
leet-code-q-25.java
File metadata and controls
67 lines (63 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class FoodRatings {
HashMap<String, Integer> foodRating = new HashMap<>();
HashMap<String, String> foodCuisine = new HashMap<>();
HashMap<String, TreeMap<Integer, TreeSet<String>>> cuisineRatingMap = new HashMap<>();
public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
int size = foods.length;
for(int i = 0; i < size; i++) {
String food = foods[i];
String cuisine = cuisines[i];
Integer rating = ratings[i];
foodCuisine.put(food, cuisine);
foodRating.put(food, rating);
TreeMap<Integer, TreeSet<String>> cuisineRating;
if(cuisineRatingMap.containsKey(cuisine)) {
cuisineRating = cuisineRatingMap.get(cuisine);
} else {
cuisineRating = new TreeMap<>();
}
TreeSet<String> foodSet;
if(cuisineRating.containsKey(rating)) {
foodSet = cuisineRating.get(rating);
} else {
foodSet = new TreeSet<>();
}
foodSet.add(food);
cuisineRating.put(rating, foodSet);
cuisineRatingMap.put(cuisine, cuisineRating);
}
}
public void changeRating(String food, int newRating) {
int prevFoodRating = foodRating.get(food);
String cuisine = foodCuisine.get(food);
TreeMap<Integer, TreeSet<String>> cuisineRating = cuisineRatingMap.get(cuisine);
TreeSet<String> prevFoodSet = cuisineRating.get(prevFoodRating);
prevFoodSet.remove(food);
foodRating.put(food, newRating);
if(prevFoodSet.isEmpty()) {
cuisineRating.remove(prevFoodRating);
}
TreeSet<String> newFoodSet;
if(cuisineRating.containsKey(newRating)){
newFoodSet = cuisineRating.get(newRating);
} else {
newFoodSet = new TreeSet<>();
}
newFoodSet.add(food);
cuisineRating.put(newRating, newFoodSet);
}
public String highestRated(String cuisine) {
TreeMap<Integer, TreeSet<String>> cuisineRating = cuisineRatingMap.get(cuisine);
Integer rating = cuisineRating.lastKey();
TreeSet<String> foods = cuisineRating.get(rating);
String food = foods.getFirst();
System.out.println(food);
return food;
}
}
/**
* 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);
*/