{"id":3252,"date":"2018-07-22T03:19:58","date_gmt":"2018-07-22T10:19:58","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3252"},"modified":"2018-09-04T02:02:21","modified_gmt":"2018-09-04T09:02:21","slug":"leetcode-873-length-of-longest-fibonacci-subsequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-873-length-of-longest-fibonacci-subsequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 873. Length of Longest Fibonacci Subsequence"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 873. Length of Longest Fibonacci Subsequence - \u5237\u9898\u627e\u5de5\u4f5c EP210\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/Py3Jj0M1McY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>A sequence\u00a0<code>X_1, X_2, ..., X_n<\/code>\u00a0is\u00a0<em>fibonacci-like<\/em>\u00a0if:<\/p>\n<ul>\n<li><code>n &gt;= 3<\/code><\/li>\n<li><code>X_i + X_{i+1} = X_{i+2}<\/code>\u00a0for all\u00a0<code>i + 2 &lt;= n<\/code><\/li>\n<\/ul>\n<p>Given a\u00a0<b>strictly increasing<\/b>\u00a0array\u00a0<code>A<\/code>\u00a0of positive integers forming a sequence, find the\u00a0<strong>length<\/strong>\u00a0of the longest fibonacci-like subsequence of\u00a0<code>A<\/code>.\u00a0 If one does not exist, return 0.<\/p>\n<p>(<em>Recall that a subsequence is derived from another sequence\u00a0<code>A<\/code>\u00a0by\u00a0deleting any number of\u00a0elements (including none)\u00a0from\u00a0<code>A<\/code>, without changing the order of the remaining elements.\u00a0 For example,\u00a0<code>[3, 5, 8]<\/code>\u00a0is a subsequence of\u00a0<code>[3, 4, 5, 6, 7, 8]<\/code>.<\/em>)<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>[1,2,3,4,5,6,7,8]\r\n<strong>Output: <\/strong>5\r\n<strong>Explanation:\r\n<\/strong>The longest subsequence that is fibonacci-like: [1,2,3,5,8].\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>[1,3,7,11,12,14,18]\r\n<strong>Output: <\/strong>3\r\n<strong>Explanation<\/strong>:\r\nThe longest subsequence that is fibonacci-like:\r\n[1,11,12], [3,11,14] or [7,11,18].\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>3 &lt;= A.length &lt;= 1000<\/code><\/li>\n<li><code>1 &lt;= A[0] &lt; A[1] &lt; ... &lt; A[A.length - 1] &lt;= 10^9<\/code><\/li>\n<li><em>(The time limit has been reduced by 50% for submissions in Java, C, and C++.)<\/em><\/li>\n<\/ul>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3274\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/873-ep210.png\" alt=\"\" width=\"960\" height=\"540\" \/><\/h1>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\">\u00a0<\/ins><\/p>\n<h1><strong>Solution 1: DP<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 68 ms\r\n\/\/\r\n\/\/ w\/ Hashtable\r\n\/\/ Time complexity: O(n^2)\r\n\/\/ Space complexity: O(n^2)\r\nclass Solution {\r\npublic:\r\n  int lenLongestFibSubseq(vector&lt;int&gt;&amp; A) {\r\n    const int n = A.size();        \r\n    unordered_map&lt;int, int&gt; m;\r\n    for (int i = 0; i &lt; n; ++i)\r\n      m[A[i]] = i;\r\n    vector&lt;vector&lt;int&gt;&gt; dp(n, vector&lt;int&gt;(n, 2));\r\n    int ans = 0;\r\n    for (int j = 0; j &lt; n; ++j)\r\n      for (int k = j + 1; k &lt; n; ++k) {\r\n        int a_i = A[k] - A[j];\r\n        if (a_i &gt;= A[j]) break; \/\/ pruning 168 ms -&gt; 68 ms\r\n        auto it = m.find(a_i);\r\n        if (it == end(m)) continue;\r\n        int i = it-&gt;second;\r\n        dp[j][k] = dp[i][j] + 1;        \r\n        ans = max(ans, dp[j][k]);\r\n      }\r\n    return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ V2<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 96 ms\r\n\/\/ w \/ Binary Search\r\n\/\/ Time complexity: O(n^2logn)\r\n\/\/ Space complexity: O(n)\r\nclass Solution {\r\npublic:\r\n  int lenLongestFibSubseq(vector&lt;int&gt;&amp; A) {\r\n    const int n = A.size();    \r\n    vector&lt;vector&lt;int&gt;&gt; dp(n, vector&lt;int&gt;(n, 2));\r\n    int ans = 0;\r\n    for (int j = 0; j &lt; n; ++j)\r\n      for (int k = j + 1; k &lt; n; ++k) {\r\n        int a_i = A[k] - A[j];\r\n        if (a_i &gt;= A[j]) break;\r\n        auto it = lower_bound(begin(A), begin(A) + j, a_i);\r\n        int i = it - begin(A);\r\n        if (A[i] != a_i) continue;\r\n        dp[j][k] = dp[i][j] + 1;  \r\n        ans = max(ans, dp[j][k]);\r\n      }\r\n    return ans;          \r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: HashTable<\/strong><\/h1>\n<p>Time complexity: O(n^3)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 172 ms\r\nclass Solution {\r\npublic:\r\n  int lenLongestFibSubseq(vector&lt;int&gt;&amp; A) {\r\n    const int n = A.size();    \r\n    unordered_set&lt;int&gt; m(begin(A), end(A));    \r\n    int ans = 0;\r\n    for (int i = 0; i &lt; n; ++i)\r\n      for (int j = i + 1; j &lt; n; ++j) {\r\n        int a = A[i];\r\n        int b = A[j];\r\n        int c = a + b;\r\n        int l = 2;\r\n        while (m.count(c)) {\r\n          a = b;\r\n          b = c;\r\n          c = a + b;\r\n          ans = max(ans, ++l);\r\n        }\r\n      }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem A sequence\u00a0X_1, X_2, &#8230;, X_n\u00a0is\u00a0fibonacci-like\u00a0if: n &gt;= 3 X_i + X_{i+1} = X_{i+2}\u00a0for all\u00a0i + 2 &lt;= n Given a\u00a0strictly increasing\u00a0array\u00a0A\u00a0of positive integers forming&#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,70],"tags":[18,345,82,177],"class_list":["post-3252","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-hashtable","tag-dp","tag-fibonacci","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3252","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=3252"}],"version-history":[{"count":11,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3252\/revisions"}],"predecessor-version":[{"id":3852,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3252\/revisions\/3852"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}