{"id":2868,"date":"2018-05-28T17:09:10","date_gmt":"2018-05-29T00:09:10","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2868"},"modified":"2018-05-28T17:34:05","modified_gmt":"2018-05-29T00:34:05","slug":"leetcode-334-increasing-triplet-subsequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-334-increasing-triplet-subsequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 334. Increasing Triplet Subsequence"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u5224\u65ad\u4e00\u4e2a\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u957f\u5ea6\u4e3a3\u7684\u5355\u8c03\u9012\u589e\u5b50\u5e8f\u5217\u3002<\/p>\n<p>Formally the function should:<\/p>\n<blockquote><p>Return true if there exists\u00a0<i>i, j, k\u00a0<\/i><br \/>\nsuch that\u00a0<i>arr[i]<\/i>\u00a0&lt;\u00a0<i>arr[j]<\/i>\u00a0&lt;\u00a0<i>arr[k]<\/i>\u00a0given 0 \u2264\u00a0<i>i<\/i>\u00a0&lt;\u00a0<i>j<\/i>\u00a0&lt;\u00a0<i>k<\/i>\u00a0\u2264\u00a0<i>n<\/i>-1 else return false.<\/p><\/blockquote>\n<p>Your algorithm should run in O(<i>n<\/i>) time complexity and O(<i>1<\/i>) space complexity.<\/p>\n<p><b>Examples:<\/b><br \/>\nGiven\u00a0<code>[1, 2, 3, 4, 5]<\/code>,<br \/>\nreturn\u00a0<code>true<\/code>.<\/p>\n<p>Given\u00a0<code>[5, 4, 3, 2, 1]<\/code>,<br \/>\nreturn\u00a0<code>false<\/code>.<\/p>\n<p><b>Credits:<\/b><br \/>\nSpecial thanks to\u00a0<a href=\"https:\/\/leetcode.com\/discuss\/user\/DjangoUnchained\">@DjangoUnchained<\/a>\u00a0for adding this problem and creating all test cases.<\/p>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2871\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/334-ep193.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/334-ep193.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/334-ep193-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/05\/334-ep193-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><strong>Solution: Greedy<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 7 ms\r\nclass Solution {\r\npublic:\r\n  bool increasingTriplet(vector&lt;int&gt;&amp; nums) {\r\n    int min1 = INT_MAX;\r\n    int min2 = INT_MAX;\r\n    for (int num : nums) {\r\n      if (num &gt; min2) return true;\r\n      else if (num &lt; min1) {\r\n        min1 = num;\r\n      } else if (num &gt; min1 &amp;&amp; num &lt; min2) {\r\n        min2 = num;\r\n      }\r\n    }\r\n    return false;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u5224\u65ad\u4e00\u4e2a\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u957f\u5ea6\u4e3a3\u7684\u5355\u8c03\u9012\u589e\u5b50\u5e8f\u5217\u3002 Formally the function should: Return true if there exists\u00a0i, j, k\u00a0 such that\u00a0arr[i]\u00a0&lt;\u00a0arr[j]\u00a0&lt;\u00a0arr[k]\u00a0given 0 \u2264\u00a0i\u00a0&lt;\u00a0j\u00a0&lt;\u00a0k\u00a0\u2264\u00a0n-1 else return false. Your algorithm should run in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[305,229],"class_list":["post-2868","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-increasing","tag-subsequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2868","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=2868"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2868\/revisions"}],"predecessor-version":[{"id":2872,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2868\/revisions\/2872"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}