<?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>stream Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/stream/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/stream/</link>
	<description></description>
	<lastBuildDate>Fri, 13 Jul 2018 03:40:25 +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>stream Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/stream/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 703. Kth Largest Element in a Stream</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-703-kth-largest-element-in-a-stream/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-703-kth-largest-element-in-a-stream/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 13 Jul 2018 03:33:59 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[stream]]></category>
		<category><![CDATA[top k]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3095</guid>

					<description><![CDATA[<p>Problem Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-703-kth-largest-element-in-a-stream/">花花酱 LeetCode 703. Kth Largest Element in a Stream</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Design a class to find the <strong>k</strong>th largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.</p>
<p>Your <code>KthLargest</code> class will have a constructor which accepts an integer <code>k</code> and an integer array <code>nums</code>, which contains initial elements from the stream. For each call to the method <code>KthLargest.add</code>, return the element representing the kth largest element in the stream.</p>
<p><strong>Example:</strong></p>
<pre class="crayon:false">int k = 3;
int[] arr = [4,5,8,2];
KthLargest kthLargest = new KthLargest(3, arr);
kthLargest.add(3);   // returns 4
kthLargest.add(5);   // returns 5
kthLargest.add(10);  // returns 5
kthLargest.add(9);   // returns 8
kthLargest.add(4);   // returns 8
</pre>
<p><strong>Note: </strong><br />
You may assume that <code>nums</code>&#8216; length ≥ <code>k-1</code> and <code>k</code> ≥ 1.</p>
<h1><strong>Solution: BST / Min Heap</strong></h1>
<p>Time complexity: O(nlogk)</p>
<p>Space complexity: O(k)</p>
<p>C++ / BST</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 32 ms
class KthLargest {
public:
  KthLargest(int k, vector&lt;int&gt; nums): k_(k) {
    for (int num : nums)
      add(num);
  }

  int add(int val) {    
    s_.insert(val);
    if (s_.size() &gt; k_)
      s_.erase(s_.begin());
    return *s_.begin();
  }
private:
  const int k_;  
  multiset&lt;int&gt; s_;
};</pre><p>C++ / Min Heap</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 28 ms
class KthLargest {
public:
  KthLargest(int k, vector&lt;int&gt; nums): k_(k) {    
    for (int num : nums)
      add(num);
  }

  int add(int val) {    
    s_.push(val);
    if (s_.size() &gt; k_)
      s_.pop();
    return s_.top();
  }
private:
  const int k_;  
  priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; s_; // min heap
};</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-703-kth-largest-element-in-a-stream/">花花酱 LeetCode 703. Kth Largest Element in a Stream</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/heap/leetcode-703-kth-largest-element-in-a-stream/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 295. Find Median from Data Stream O(logn) + O(1)</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-295-find-median-from-data-stream/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-295-find-median-from-data-stream/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 12 Sep 2017 08:01:52 +0000</pubDate>
				<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[Heap]]></category>
		<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[median]]></category>
		<category><![CDATA[online]]></category>
		<category><![CDATA[priority queue]]></category>
		<category><![CDATA[stream]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=243</guid>

					<description><![CDATA[<p>Problem: Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-295-find-median-from-data-stream/">花花酱 LeetCode 295. Find Median from Data Stream O(logn) + O(1)</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><iframe width="500" height="375" src="https://www.youtube.com/embed/60xnYZ21Ir0?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.</p>
<p>Examples:</p>
<p><code>[2,3,4]</code> , the median is <code>3</code></p>
<p><code>[2,3]</code>, the median is <code>(2 + 3) / 2 = 2.5</code></p>
<p>Design a data structure that supports the following two operations:</p>
<ul>
<li>void addNum(int num) &#8211; Add a integer number from the data stream to the data structure.</li>
<li>double findMedian() &#8211; Return the median of all elements so far.</li>
</ul>
<p>For example:</p><pre class="crayon-plain-tag">addNum(1)
addNum(2)
findMedian() = 1.5
addNum(3) 
findMedian() = 2</pre><p>&nbsp;</p>
<p><strong>Idea</strong>:</p>
<ol>
<li>Min/Max heap</li>
<li>Balanced binary search tree</li>
</ol>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1.png"><img class="alignnone size-full wp-image-248" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a><img class="alignnone size-full wp-image-247" style="font-size: 1rem;" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-2-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-2-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3.png"><img class="alignnone size-full wp-image-246" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Time Complexity</strong>:</p>
<p>add(num): O(logn)</p>
<p>findMedian(): O(logn)</p>
<p><strong>Solution1</strong>:</p><pre class="crayon-plain-tag">// Author: Huahua
// Running Time: 152 ms
class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder() {}
    
    // l_.size() &gt;= r_.size()
    void addNum(int num) {
        if (l_.empty() || num &lt;= l_.top()) {
            l_.push(num);
        } else {
            r_.push(num);
        }
        
        // Step 2: Balence left/right
        if (l_.size() &lt; r_.size()) {
            l_.push(r_.top());
            r_.pop();
        } else if (l_.size() - r_.size() == 2) {
            r_.push(l_.top());
            l_.pop();
        }
    }
    
    double findMedian() {
        if (l_.size() &gt; r_.size()) {
            return static_cast&lt;double&gt;(l_.top());
        } else {            
            return (static_cast&lt;double&gt;(l_.top()) + r_.top()) / 2;
        }
    }
private:
    priority_queue&lt;int, vector&lt;int&gt;, less&lt;int&gt;&gt; l_;    // max-heap
    priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; r_; // min-heap
};</pre><p>&nbsp;</p>
<p><strong>Solution 2:</strong></p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 172 ms
class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder(): l_(m_.cend()), r_(m_.cend()) {}
    
    // O(logn)
    void addNum(int num) {
        if (m_.empty()) {
            l_ = r_ = m_.insert(num);
            return;
        }
        
        m_.insert(num);
        const size_t n = m_.size();    
        
        if (n &amp; 1) {
            // odd number
            if (num &gt;= *r_) {         
                l_ = r_;
            } else {
                // num &lt; *r_, l_ could be invalidated
                l_ = --r_;
            }
        } else {
            if (num &gt;= *r_)
                ++r_;
            else
                --l_;
        }
    }
    // O(1)
    double findMedian() {
        return (static_cast&lt;double&gt;(*l_) + *r_) / 2;
    }
private:
    multiset&lt;int&gt; m_;
    multiset&lt;int&gt;::const_iterator l_;  // current left median
    multiset&lt;int&gt;::const_iterator r_;  // current right median
};</pre><p>&nbsp;</p>
<p><strong>Related Problems</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/difficulty/hard/leetcode-480-sliding-window-median/">花花酱 LeetCode 480. Sliding Window Median</a></li>
<li><a href="http://zxi.mytechroad.com/blog/binary-search/leetcode-4-median-of-two-sorted-arrays/">[解题报告] LeetCode 4. Median of Two Sorted Arrays</a></li>
<li><a href="http://zxi.mytechroad.com/blog/zoj/zoj-3612-median/">[ZOJ] 3612: Median</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-295-find-median-from-data-stream/">花花酱 LeetCode 295. Find Median from Data Stream O(logn) + O(1)</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/leetcode/leetcode-295-find-median-from-data-stream/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
