<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>orderedset Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/orderedset/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/orderedset/</link>
	<description></description>
	<lastBuildDate>Sun, 30 Mar 2025 00:40:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.9</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>orderedset Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/orderedset/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2353. Design a Food Rating System</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2353-design-a-food-rating-system/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2353-design-a-food-rating-system/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Mar 2025 00:38:41 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[orderedset]]></category>
		<category><![CDATA[treeset]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10241</guid>

					<description><![CDATA[<p>Hashtable + TreeSet / OrderedSet 用了三个Hashtable&#8230; 菜名 -> 菜系 菜系 -> Treemap 菜名 -> 所在Treemap中的迭代器 每个菜系用一个TreeSet来保存从高到低的菜色排名，查询的时候只要拿出第一个就好了。 修改的时候，拿出迭代器，删除原来的评分，再把新的评分加入（别忘了更新迭代器） 时间复杂度：构造O(nlogn)，修改O(logn)，查询O(1) 空间复杂度：O(n) [crayon-67e8cd0a7b2f7589033751/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2353-design-a-food-rating-system/">花花酱 LeetCode 2353. Design a Food Rating System</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Hashtable + TreeSet / OrderedSet</p>



<p>用了三个Hashtable&#8230;</p>



<ol><li>菜名 -> 菜系</li><li>菜系 -> Treemap</li><li>菜名 -> 所在Treemap中的迭代器</li></ol>



<p>每个菜系用一个TreeSet来保存从高到低的菜色排名，查询的时候只要拿出第一个就好了。</p>



<p>修改的时候，拿出迭代器，删除原来的评分，再把新的评分加入（别忘了更新迭代器）</p>



<p>时间复杂度：构造O(nlogn)，修改O(logn)，查询O(1)</p>



<p>空间复杂度：O(n)</p>



<pre class="crayon-plain-tag">class FoodRatings {
public:
  FoodRatings(vector&lt;string&gt;&amp; foods, vector&lt;string&gt;&amp; cuisines, vector&lt;int&gt;&amp; ratings) {
    const int n = foods.size();
    for (int i = 0; i &lt; n; ++i) {
      n_[foods[i]] = cuisines[i];
      m_[foods[i]] = c_[cuisines[i]].emplace(-ratings[i], foods[i]).first;
    }
  }
  
  void changeRating(string food, int newRating) {
    const string&amp; cuisine = n_[food];
    c_[cuisine].erase(m_[food]);
    m_[food] = c_[cuisine].emplace(-newRating, food).first;
  }
  
  string highestRated(string cuisine) {
    return begin(c_[cuisine])-&gt;second;
  }
private:
  unordered_map&lt;string, set&lt;pair&lt;int, string&gt;&gt;&gt; c_; // cuisine -&gt; {{-rating, name}}
  unordered_map&lt;string, string&gt; n_; // food -&gt; cuisine  
  unordered_map&lt;string, set&lt;pair&lt;int, string&gt;&gt;::iterator&gt; m_; // food -&gt; set iterator
};

/**
 * Your FoodRatings object will be instantiated and called as such:
 * FoodRatings* obj = new FoodRatings(foods, cuisines, ratings);
 * obj-&gt;changeRating(food,newRating);
 * string param_2 = obj-&gt;highestRated(cuisine);
 */</pre>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2353-design-a-food-rating-system/">花花酱 LeetCode 2353. Design a Food Rating System</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-2353-design-a-food-rating-system/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
