{"id":4668,"date":"2019-01-20T00:42:50","date_gmt":"2019-01-20T08:42:50","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4668"},"modified":"2019-01-20T00:46:21","modified_gmt":"2019-01-20T08:46:21","slug":"leetcode-978-longest-turbulent-subarray","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-978-longest-turbulent-subarray\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 978. Longest Turbulent Subarray"},"content":{"rendered":"\n<p>A subarray&nbsp;<code>A[i], A[i+1], ..., A[j]<\/code>&nbsp;of&nbsp;<code>A<\/code>&nbsp;is said to be&nbsp;<em>turbulent<\/em>&nbsp;if and only if:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For&nbsp;<code>i &lt;= k &lt; j<\/code>,&nbsp;<code>A[k] &gt; A[k+1]<\/code>&nbsp;when&nbsp;<code>k<\/code>&nbsp;is odd, and&nbsp;<code>A[k] &lt; A[k+1]<\/code>&nbsp;when&nbsp;<code>k<\/code>&nbsp;is even;<\/li><li><strong>OR<\/strong>, for&nbsp;<code>i &lt;= k &lt; j<\/code>,&nbsp;<code>A[k] &gt; A[k+1]<\/code>&nbsp;when&nbsp;<code>k<\/code>&nbsp;is even, and&nbsp;<code>A[k] &lt; A[k+1]<\/code>when&nbsp;<code>k<\/code>&nbsp;is odd.<\/li><\/ul>\n\n\n\n<p>That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.<\/p>\n\n\n\n<p>Return the&nbsp;<strong>length<\/strong>&nbsp;of a&nbsp;maximum size turbulent subarray of A.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[9,4,2,10,7,8,8,1,9]\n<strong>Output: <\/strong>5\n<strong>Explanation: <\/strong>(A[1] &gt; A[2] &lt; A[3] &gt; A[4] &lt; A[5])\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[4,8,12,16]\n<strong>Output: <\/strong>2\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[100]\n<strong>Output: <\/strong>1\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;= 40000<\/code><\/li><li><code>0 &lt;= A[i] &lt;= 10^9<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: DP<\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) -> O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, running time: 120 ms\nclass Solution {\npublic:\n  int maxTurbulenceSize(vector<int>& A) {\n    vector<int> up(A.size(), 1);\n    vector<int> down(A.size(), 1);\n    int ans = 1;\n    for (int i = 1; i < A.size(); ++i) {\n      if (A[i] > A[i - 1]) up[i] = down[i - 1] + 1;\n      if (A[i] < A[i - 1]) down[i] = up[i - 1] + 1;\n      ans = max(ans, max(up[i], down[i]));      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A subarray&nbsp;A[i], A[i+1], &#8230;, A[j]&nbsp;of&nbsp;A&nbsp;is said to be&nbsp;turbulent&nbsp;if and only if: For&nbsp;i &lt;= k &lt; j,&nbsp;A[k] &gt; A[k+1]&nbsp;when&nbsp;k&nbsp;is odd, and&nbsp;A[k] &lt; A[k+1]&nbsp;when&nbsp;k&nbsp;is even; OR, for&nbsp;i&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,302],"class_list":["post-4668","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-flip","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4668","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=4668"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4668\/revisions"}],"predecessor-version":[{"id":4671,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4668\/revisions\/4671"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}