Today's leetcode 3rd question

Revision en1, by anshu2002, 2022-07-24 08:22:28

Though I may sound noob . But I want to learn

class FoodRatings {
public:
    map<string,set<pair<int,string>>> s;
    unordered_map<string,string> abcd;
    unordered_map<string,int> rat;
    FoodRatings(vector<string>& ff, vector<string>& str, vector<int>& rate) {
        int i=0;
        while(i<ff.size())
        {
            s[str[i]].insert({-rate[i],ff[i]});
            abcd[ff[i]]=str[i];
            rat[ff[i]]=-rate[i];
            i++;
        }
    }
    void change(string food, int newRating){
        int r=rat[food];
        string c=abcd[food];
        s[c].erase({r,food});
        s[c].insert({-newRating,food});
        rat[food]=-newRating;
    }
    void changeRating(string food, int newRating) {
        change(food,newRating);
    }
    
    string highestRated(string cuisine) {
        pair<int,string> p= *(s[cuisine].begin());
        return p.second;
    }
};

It was one of the submission of todays weekly contest ( number 303 ) . So I want to know that how the 's' map , when that encounter a food of a cuisine with already the highest rating . How it ensures that it finally excepts the lexicographically smaller

Tags leetcode, weekly contest, 303

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English anshu2002 2022-07-24 08:22:28 1228 Initial revision (published)