{"id":2170,"date":"2018-03-17T13:07:54","date_gmt":"2018-03-17T20:07:54","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2170"},"modified":"2018-03-17T13:18:13","modified_gmt":"2018-03-17T20:18:13","slug":"leetcode-414-third-maximum-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-414-third-maximum-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 414. Third Maximum Number"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/third-maximum-number\/description\/\">https:\/\/leetcode.com\/problems\/third-maximum-number\/description\/<\/a><\/p>\n<p>Given a\u00a0<b>non-empty<\/b>\u00a0array of integers, return the\u00a0<b>third<\/b>\u00a0maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> [3, 2, 1]\r\n\r\n<b>Output:<\/b> 1\r\n\r\n<b>Explanation:<\/b> The third maximum is 1.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> [1, 2]\r\n\r\n<b>Output:<\/b> 2\r\n\r\n<b>Explanation:<\/b> The third maximum does not exist, so the maximum (2) is returned instead.\r\n<\/pre>\n<p><b>Example 3:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> [2, 2, 3, 1]\r\n\r\n<b>Output:<\/b> 1\r\n\r\n<b>Explanation:<\/b> Note that the third maximum here means the third maximum distinct number.\r\nBoth numbers with value 2 are both considered as second maximum.<\/pre>\n<h1><strong>Solution: Set<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 9 ms\r\nclass Solution {\r\npublic:\r\n  int thirdMax(vector&lt;int&gt;&amp; nums) {\r\n    set&lt;int&gt; s;\r\n    for (int num : nums) {\r\n      s.insert(num);\r\n      if (s.size() &gt; 3) s.erase(s.begin());\r\n    }\r\n    return s.size() != 3 ? *s.rbegin() : *s.begin();\r\n  }\r\n};<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 52 ms\r\n\"\"\"\r\nclass Solution:\r\n  def thirdMax(self, nums):\r\n    s = set()\r\n    for num in nums:\r\n      s.add(num)\r\n      if len(s) &gt; 3: s.remove(min(s))\r\n    return min(s) if len(s) == 3 else max(s)<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem https:\/\/leetcode.com\/problems\/third-maximum-number\/description\/ Given a\u00a0non-empty\u00a0array of integers, return the\u00a0third\u00a0maximum number in this array. If it does not exist, return the maximum number. The time complexity must&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[222,141,259,243],"class_list":["post-2170","post","type-post","status-publish","format-standard","hentry","category-array","tag-easy","tag-max","tag-ordered","tag-set","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2170"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2170\/revisions"}],"predecessor-version":[{"id":2176,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2170\/revisions\/2176"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}