<?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>ordered set Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/ordered-set/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/ordered-set/</link>
	<description></description>
	<lastBuildDate>Wed, 14 Apr 2021 03:02:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>ordered set Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/ordered-set/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1825. Finding MK Average</title>
		<link>https://zxi.mytechroad.com/blog/data-structure/leetcode-1825-finding-mk-average/</link>
					<comments>https://zxi.mytechroad.com/blog/data-structure/leetcode-1825-finding-mk-average/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 14 Apr 2021 02:58:09 +0000</pubDate>
				<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[data structure]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[multiset]]></category>
		<category><![CDATA[ordered set]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8350</guid>

					<description><![CDATA[<p>You are given two integers,&#160;m&#160;and&#160;k, and a stream of integers. You are tasked to implement a data structure that calculates the&#160;MKAverage&#160;for the stream. The&#160;MKAverage&#160;can be&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/data-structure/leetcode-1825-finding-mk-average/">花花酱 LeetCode 1825. Finding MK Average</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>You are given two integers,&nbsp;<code>m</code>&nbsp;and&nbsp;<code>k</code>, and a stream of integers. You are tasked to implement a data structure that calculates the&nbsp;<strong>MKAverage</strong>&nbsp;for the stream.</p>



<p>The&nbsp;<strong>MKAverage</strong>&nbsp;can be calculated using these steps:</p>



<ol><li>If the number of the elements in the stream is less than&nbsp;<code>m</code>&nbsp;you should consider the&nbsp;<strong>MKAverage</strong>&nbsp;to be&nbsp;<code>-1</code>. Otherwise, copy the last&nbsp;<code>m</code>&nbsp;elements of the stream to a separate container.</li><li>Remove the smallest&nbsp;<code>k</code>&nbsp;elements and the largest&nbsp;<code>k</code>&nbsp;elements from the container.</li><li>Calculate the average value for the rest of the elements&nbsp;<strong>rounded down to the nearest integer</strong>.</li></ol>



<p>Implement the&nbsp;<code>MKAverage</code>&nbsp;class:</p>



<ul><li><code>MKAverage(int m, int k)</code>&nbsp;Initializes the&nbsp;<strong>MKAverage</strong>&nbsp;object with an empty stream and the two integers&nbsp;<code>m</code>&nbsp;and&nbsp;<code>k</code>.</li><li><code>void addElement(int num)</code>&nbsp;Inserts a new element&nbsp;<code>num</code>&nbsp;into the stream.</li><li><code>int calculateMKAverage()</code>&nbsp;Calculates and returns the&nbsp;<strong>MKAverage</strong>&nbsp;for the current stream&nbsp;<strong>rounded down to the nearest integer</strong>.</li></ul>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input</strong>
["MKAverage", "addElement", "addElement", "calculateMKAverage", "addElement", "calculateMKAverage", "addElement", "addElement", "addElement", "calculateMKAverage"]
[[3, 1], [3], [1], [], [10], [], [5], [5], [5], []]
<strong>Output</strong>
[null, null, null, -1, null, 3, null, null, null, 5]
<p><strong>Explanation</strong> MKAverage obj = new MKAverage(3, 1); obj.addElement(3); // current elements are [3] obj.addElement(1); // current elements are [3,1] obj.calculateMKAverage(); // return -1, because m = 3 and only 2 elements exist. obj.addElement(10); // current elements are [3,1,10] obj.calculateMKAverage(); // The last 3 elements are [3,1,10]. // After removing smallest and largest 1 element the container will be <code>[3]. // The average of [3] equals 3/1 = 3, return 3 obj.addElement(5); // current elements are [3,1,10,5] obj.addElement(5); // current elements are [3,1,10,5,5] obj.addElement(5); // current elements are [3,1,10,5,5,5] obj.calculateMKAverage(); // The last 3 elements are [5,5,5]. // After removing smallest and largest 1 element the container will be <code>[5]. // The average of [5] equals 5/1 = 5, return 5</code></code></p>
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>3 &lt;= m &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= k*2 &lt; m</code></li><li><code>1 &lt;= num &lt;= 10<sup>5</sup></code></li><li>At most&nbsp;<code>10<sup>5</sup></code>&nbsp;calls will be made to&nbsp;<code>addElement</code>&nbsp;and&nbsp;<code>calculateMKAverage</code>.</li></ul>



<h2><strong>Solution 1: Multiset * 3</strong></h2>



<p>Use three multiset to track the left part (smallest k elements), right part (largest k elements) and mid (middle part of m &#8211; 2*k elements).</p>



<p>Time complexity: addElememt: O(logn), average: O(1)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class MKAverage {
public:
  MKAverage(int m, int k): 
    sum(0), m(m), k(k), n(m - 2*k) {}

  void addElement(int num) {
    if (q.size() == m) {      
      remove(q.front());
      q.pop();
    }
    q.push(num);
    add(num);
  }

  int calculateMKAverage() {    
    return (q.size() &lt; m) ? -1 : sum / n;
  }
private:
  void add(int x) {
    left.insert(x);
    
    if (left.size() &gt; k) {
      auto it = prev(end(left));
      sum += *it;
      mid.insert(*it);      
      left.erase(it);
    }
    
    if (mid.size() &gt; n) {
      auto it = prev(end(mid));
      sum -= *it; 
      right.insert(*it);
      mid.erase(it);
    }
  }
  
  void remove(int x) {
    if (x &lt;= *rbegin(left)) {
      left.erase(left.find(x));
    } else if (x &lt;= *rbegin(mid)) {
      sum -= x;
      mid.erase(mid.find(x));
    } else {
      right.erase(right.find(x));
    }
    
    if (left.size() &lt; k) {
      auto it = begin(mid);
      sum -= *it;
      left.insert(*it);
      mid.erase(it);
    }
    
    if (mid.size() &lt; n) {
      auto it = begin(right);
      sum += *it;
      mid.insert(*it);
      right.erase(it);
    }
  }
  
  queue&lt;int&gt; q;
  multiset&lt;int&gt; left, mid, right;  
  long sum;
  const int m;
  const int k;
  const int n;
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/data-structure/leetcode-1825-finding-mk-average/">花花酱 LeetCode 1825. Finding MK Average</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/data-structure/leetcode-1825-finding-mk-average/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
