Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SuryaPratapK/2832e555e9e7677f0144c090280d4fd6 to your computer and use it in GitHub Desktop.

Select an option

Save SuryaPratapK/2832e555e9e7677f0144c090280d4fd6 to your computer and use it in GitHub Desktop.
class FoodRatings {
#define pis pair<int,string>
struct compare {
bool operator()(const pis& a, const pis& b) const {
if (a.first != b.first)
return a.first < b.first; // higher rating = higher priority
return a.second > b.second; // lexicographically smaller name = higher priority
}
};
unordered_map<string,pis> food_rating_cuisine;
unordered_map<string,priority_queue<pis,vector<pis>,compare>> cuisine_maxheap;
public:
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
for(int i=0;i<foods.size();++i){
food_rating_cuisine[foods[i]] = make_pair(ratings[i],cuisines[i]);
cuisine_maxheap[cuisines[i]].push({ratings[i],foods[i]});
}
}
void changeRating(string food, int newRating) {
food_rating_cuisine[food].first = newRating;
cuisine_maxheap[food_rating_cuisine[food].second].push({newRating,food});
}
string highestRated(string cuisine) {
while(!cuisine_maxheap.empty()){
pis curr = cuisine_maxheap[cuisine].top();
cuisine_maxheap[cuisine].pop();
if(food_rating_cuisine[curr.second].first == curr.first){
cuisine_maxheap[cuisine].push(curr);
return curr.second;
}
}
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);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment