anshu2002's blog

By anshu2002, history, 2 years ago, In English

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

  • Vote: I like it
  • -5
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

set<pair<int, string>> automatically sorts based on lexicographic order if two integers are the same.

»
2 years ago, # |
  Vote: I like it +3 Vote: I do not like it

cuisine map to pair of negative of rating (which ensure highest rated food at begin of map) and food (which is sorted lexicographically which ensure smallest string a begin of map).