<?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>online Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/online/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/online/</link>
	<description></description>
	<lastBuildDate>Wed, 11 Jul 2018 01:52:42 +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>online Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/online/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 495. Teemo Attacking</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-495-teemo-attacking/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-495-teemo-attacking/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 08 Mar 2018 08:06:50 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Simulation]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[online]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2024</guid>

					<description><![CDATA[<p>题目大意：给你攻击的时间序列以及中毒的时长，求总共的中毒时间。 Problem: https://leetcode.com/problems/teemo-attacking/description/ In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-495-teemo-attacking/">花花酱 LeetCode 495. Teemo Attacking</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>题目大意：给你攻击的时间序列以及中毒的时长，求总共的中毒时间。</p>
<p><strong>Problem:</strong></p>
<p><a href="https://leetcode.com/problems/teemo-attacking/description/">https://leetcode.com/problems/teemo-attacking/description/</a></p>
<p>In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo&#8217;s attacking <b>ascending</b> time series towards Ashe and the poisoning time duration per Teemo&#8217;s attacking, you need to output the total time that Ashe is in poisoned condition.</p>
<p>You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false">Input: [1,4], 2
Output: 4
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately. 
This poisoned status will last 2 seconds until the end of time point 2. 
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds. 
So you finally need to output 4.
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false ">Input: [1,2], 2
Output: 3
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned. 
This poisoned status will last 2 seconds until the end of time point 2. 
However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status. 
Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3. 
So you finally need to output 3.
</pre>
<p><b>Note:</b></p>
<ol>
<li>You may assume the length of given time series array won&#8217;t exceed 10000.</li>
<li>You may assume the numbers in the Teemo&#8217;s attacking time series and his poisoning time duration per attacking are non-negative integers, which won&#8217;t exceed 10,000,000.</li>
</ol>
<p><strong>Idea: Running Process</strong></p>
<p>Compare the current attack time with the last one, if span is more than duration, add duration to total, otherwise add (curr &#8211; last).</p>
<p><strong>C++</strong></p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 66 ms
class Solution {
public:
  int findPoisonedDuration(vector&lt;int&gt;&amp; t, int duration) {
    if (t.empty()) return 0;    
    int total = 0;
    for (int i = 1; i &lt; t.size(); ++ i)
      total += (t[i] &gt; t[i - 1] + duration ? duration : t[i] - t[i - 1]);
    return total + duration;
  }
};</pre><p>Java</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 9 ms
class Solution {
  public int findPoisonedDuration(int[] t, int duration) {
    if (t.length == 0) return 0;    
    int total = 0;
    for (int i = 1; i &lt; t.length; ++ i)
      total += (t[i] &gt; t[i - 1] + duration ? duration : t[i] - t[i - 1]);
    return total + duration;
  }
}</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 64 ms (beats 100%)
"""
class Solution:
  def findPoisonedDuration(self, timeSeries, duration):
    if not timeSeries: return 0
    l = timeSeries[0]
    total = 0
    for t in timeSeries:
      total += duration if t - l &gt; duration else t - l
      l = t
    return total + duration</pre><p></p><pre class="crayon-plain-tag">import numpy as np
class Solution:
  def findPoisonedDuration(self, timeSeries, duration):    
    if not timeSeries: return 0
    d = np.diff(timeSeries)
    d = np.clip(d, 0, duration)
    return int(np.sum(d) + duration)</pre><p></p><pre class="crayon-plain-tag">class Solution:
  def findPoisonedDuration(self, t, duration):    
    return sum([min(c - l, duration) for l, c in zip(t, t[1:] + [1e9])])</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-495-teemo-attacking/">花花酱 LeetCode 495. Teemo Attacking</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/algorithms/array/leetcode-495-teemo-attacking/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>
