<?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 Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/ordered/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/ordered/</link>
	<description></description>
	<lastBuildDate>Sat, 17 Mar 2018 20:18:13 +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 Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/ordered/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 414. Third Maximum Number</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-414-third-maximum-number/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-414-third-maximum-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 17 Mar 2018 20:07:54 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[max]]></category>
		<category><![CDATA[ordered]]></category>
		<category><![CDATA[set]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2170</guid>

					<description><![CDATA[<p>Problem https://leetcode.com/problems/third-maximum-number/description/ Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-414-third-maximum-number/">花花酱 LeetCode 414. Third Maximum Number</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><a href="https://leetcode.com/problems/third-maximum-number/description/">https://leetcode.com/problems/third-maximum-number/description/</a></p>
<p>Given a <b>non-empty</b> array of integers, return the <b>third</b> maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> [3, 2, 1]

<b>Output:</b> 1

<b>Explanation:</b> The third maximum is 1.
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false"><b>Input:</b> [1, 2]

<b>Output:</b> 2

<b>Explanation:</b> The third maximum does not exist, so the maximum (2) is returned instead.
</pre>
<p><b>Example 3:</b></p>
<pre class="crayon:false "><b>Input:</b> [2, 2, 3, 1]

<b>Output:</b> 1

<b>Explanation:</b> Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.</pre>
<h1><strong>Solution: Set</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 9 ms
class Solution {
public:
  int thirdMax(vector&lt;int&gt;&amp; nums) {
    set&lt;int&gt; s;
    for (int num : nums) {
      s.insert(num);
      if (s.size() &gt; 3) s.erase(s.begin());
    }
    return s.size() != 3 ? *s.rbegin() : *s.begin();
  }
};</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 52 ms
"""
class Solution:
  def thirdMax(self, nums):
    s = set()
    for num in nums:
      s.add(num)
      if len(s) &gt; 3: s.remove(min(s))
    return min(s) if len(s) == 3 else max(s)</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-414-third-maximum-number/">花花酱 LeetCode 414. Third Maximum Number</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-414-third-maximum-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
