{"id":10241,"date":"2025-03-29T17:38:41","date_gmt":"2025-03-30T00:38:41","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10241"},"modified":"2025-03-29T17:40:06","modified_gmt":"2025-03-30T00:40:06","slug":"leetcode-2353-design-a-food-rating-system","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2353-design-a-food-rating-system\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2353. Design a Food Rating System"},"content":{"rendered":"\n<p>Hashtable + TreeSet \/ OrderedSet<\/p>\n\n\n\n<p>\u7528\u4e86\u4e09\u4e2aHashtable&#8230;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u83dc\u540d -> \u83dc\u7cfb<\/li><li>\u83dc\u7cfb -> Treemap<\/li><li>\u83dc\u540d -> \u6240\u5728Treemap\u4e2d\u7684\u8fed\u4ee3\u5668<\/li><\/ol>\n\n\n\n<p>\u6bcf\u4e2a\u83dc\u7cfb\u7528\u4e00\u4e2aTreeSet\u6765\u4fdd\u5b58\u4ece\u9ad8\u5230\u4f4e\u7684\u83dc\u8272\u6392\u540d\uff0c\u67e5\u8be2\u7684\u65f6\u5019\u53ea\u8981\u62ff\u51fa\u7b2c\u4e00\u4e2a\u5c31\u597d\u4e86\u3002<\/p>\n\n\n\n<p>\u4fee\u6539\u7684\u65f6\u5019\uff0c\u62ff\u51fa\u8fed\u4ee3\u5668\uff0c\u5220\u9664\u539f\u6765\u7684\u8bc4\u5206\uff0c\u518d\u628a\u65b0\u7684\u8bc4\u5206\u52a0\u5165\uff08\u522b\u5fd8\u4e86\u66f4\u65b0\u8fed\u4ee3\u5668\uff09<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6784\u9020O(nlogn)\uff0c\u4fee\u6539O(logn)\uff0c\u67e5\u8be2O(1)<\/p>\n\n\n\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n)<\/p>\n\n\n\n<pre lang=\"c++\">\nclass FoodRatings {\npublic:\n  FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {\n    const int n = foods.size();\n    for (int i = 0; i < n; ++i) {\n      n_[foods[i]] = cuisines[i];\n      m_[foods[i]] = c_[cuisines[i]].emplace(-ratings[i], foods[i]).first;\n    }\n  }\n  \n  void changeRating(string food, int newRating) {\n    const string&#038; cuisine = n_[food];\n    c_[cuisine].erase(m_[food]);\n    m_[food] = c_[cuisine].emplace(-newRating, food).first;\n  }\n  \n  string highestRated(string cuisine) {\n    return begin(c_[cuisine])->second;\n  }\nprivate:\n  unordered_map<string, set<pair<int, string>>> c_; \/\/ cuisine -> {{-rating, name}}\n  unordered_map<string, string> n_; \/\/ food -> cuisine  \n  unordered_map<string, set<pair<int, string>>::iterator> m_; \/\/ food -> set iterator\n};\n\n\/**\n * Your FoodRatings object will be instantiated and called as such:\n * FoodRatings* obj = new FoodRatings(foods, cuisines, ratings);\n * obj->changeRating(food,newRating);\n * string param_2 = obj->highestRated(cuisine);\n *\/\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hashtable + TreeSet \/ OrderedSet \u7528\u4e86\u4e09\u4e2aHashtable&#8230; \u83dc\u540d -> \u83dc\u7cfb \u83dc\u7cfb -> Treemap \u83dc\u540d -> \u6240\u5728Treemap\u4e2d\u7684\u8fed\u4ee3\u5668 \u6bcf\u4e2a\u83dc\u7cfb\u7528\u4e00\u4e2aTreeSet\u6765\u4fdd\u5b58\u4ece\u9ad8\u5230\u4f4e\u7684\u83dc\u8272\u6392\u540d\uff0c\u67e5\u8be2\u7684\u65f6\u5019\u53ea\u8981\u62ff\u51fa\u7b2c\u4e00\u4e2a\u5c31\u597d\u4e86\u3002 \u4fee\u6539\u7684\u65f6\u5019\uff0c\u62ff\u51fa\u8fed\u4ee3\u5668\uff0c\u5220\u9664\u539f\u6765\u7684\u8bc4\u5206\uff0c\u518d\u628a\u65b0\u7684\u8bc4\u5206\u52a0\u5165\uff08\u522b\u5fd8\u4e86\u66f4\u65b0\u8fed\u4ee3\u5668\uff09 \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6784\u9020O(nlogn)\uff0c\u4fee\u6539O(logn)\uff0c\u67e5\u8be2O(1) \u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n) class FoodRatings { public: FoodRatings(vector&#038; foods,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,625,833,656],"class_list":["post-10241","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-iterator","tag-orderedset","tag-treeset","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10241"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10241\/revisions"}],"predecessor-version":[{"id":10243,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10241\/revisions\/10243"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}