<?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>Fibonacci Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/fibonacci/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/fibonacci/</link>
	<description></description>
	<lastBuildDate>Tue, 04 Sep 2018 09:02:21 +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>Fibonacci Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/fibonacci/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 873. Length of Longest Fibonacci Subsequence</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-873-length-of-longest-fibonacci-subsequence/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-873-length-of-longest-fibonacci-subsequence/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 22 Jul 2018 10:19:58 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[Fibonacci]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3252</guid>

					<description><![CDATA[<p>Problem A sequence X_1, X_2, ..., X_n is fibonacci-like if: n &#62;= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 &#60;= n Given a strictly increasing array A of positive integers forming&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-873-length-of-longest-fibonacci-subsequence/">花花酱 LeetCode 873. Length of Longest Fibonacci Subsequence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/Py3Jj0M1McY?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>A sequence <code>X_1, X_2, ..., X_n</code> is <em>fibonacci-like</em> if:</p>
<ul>
<li><code>n &gt;= 3</code></li>
<li><code>X_i + X_{i+1} = X_{i+2}</code> for all <code>i + 2 &lt;= n</code></li>
</ul>
<p>Given a <b>strictly increasing</b> array <code>A</code> of positive integers forming a sequence, find the <strong>length</strong> of the longest fibonacci-like subsequence of <code>A</code>.  If one does not exist, return 0.</p>
<p>(<em>Recall that a subsequence is derived from another sequence <code>A</code> by deleting any number of elements (including none) from <code>A</code>, without changing the order of the remaining elements.  For example, <code>[3, 5, 8]</code> is a subsequence of <code>[3, 4, 5, 6, 7, 8]</code>.</em>)</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>[1,2,3,4,5,6,7,8]
<strong>Output: </strong>5
<strong>Explanation:
</strong>The longest subsequence that is fibonacci-like: [1,2,3,5,8].
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>[1,3,7,11,12,14,18]
<strong>Output: </strong>3
<strong>Explanation</strong>:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>3 &lt;= A.length &lt;= 1000</code></li>
<li><code>1 &lt;= A[0] &lt; A[1] &lt; ... &lt; A[A.length - 1] &lt;= 10^9</code></li>
<li><em>(The time limit has been reduced by 50% for submissions in Java, C, and C++.)</em></li>
</ul>
<h1><img 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>
<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"> </ins></p>
<h1><strong>Solution 1: DP</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 68 ms
//
// w/ Hashtable
// Time complexity: O(n^2)
// Space complexity: O(n^2)
class Solution {
public:
  int lenLongestFibSubseq(vector&lt;int&gt;&amp; A) {
    const int n = A.size();        
    unordered_map&lt;int, int&gt; m;
    for (int i = 0; i &lt; n; ++i)
      m[A[i]] = i;
    vector&lt;vector&lt;int&gt;&gt; dp(n, vector&lt;int&gt;(n, 2));
    int ans = 0;
    for (int j = 0; j &lt; n; ++j)
      for (int k = j + 1; k &lt; n; ++k) {
        int a_i = A[k] - A[j];
        if (a_i &gt;= A[j]) break; // pruning 168 ms -&gt; 68 ms
        auto it = m.find(a_i);
        if (it == end(m)) continue;
        int i = it-&gt;second;
        dp[j][k] = dp[i][j] + 1;        
        ans = max(ans, dp[j][k]);
      }
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">C++ V2</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 96 ms
// w / Binary Search
// Time complexity: O(n^2logn)
// Space complexity: O(n)
class Solution {
public:
  int lenLongestFibSubseq(vector&lt;int&gt;&amp; A) {
    const int n = A.size();    
    vector&lt;vector&lt;int&gt;&gt; dp(n, vector&lt;int&gt;(n, 2));
    int ans = 0;
    for (int j = 0; j &lt; n; ++j)
      for (int k = j + 1; k &lt; n; ++k) {
        int a_i = A[k] - A[j];
        if (a_i &gt;= A[j]) break;
        auto it = lower_bound(begin(A), begin(A) + j, a_i);
        int i = it - begin(A);
        if (A[i] != a_i) continue;
        dp[j][k] = dp[i][j] + 1;  
        ans = max(ans, dp[j][k]);
      }
    return ans;          
  }
};</pre><p></div></div></p>
<h1><strong>Solution 2: HashTable</strong></h1>
<p>Time complexity: O(n^3)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 172 ms
class Solution {
public:
  int lenLongestFibSubseq(vector&lt;int&gt;&amp; A) {
    const int n = A.size();    
    unordered_set&lt;int&gt; m(begin(A), end(A));    
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j) {
        int a = A[i];
        int b = A[j];
        int c = a + b;
        int l = 2;
        while (m.count(c)) {
          a = b;
          b = c;
          c = a + b;
          ans = max(ans, ++l);
        }
      }
    return ans;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-873-length-of-longest-fibonacci-subsequence/">花花酱 LeetCode 873. Length of Longest Fibonacci Subsequence</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/dynamic-programming/leetcode-873-length-of-longest-fibonacci-subsequence/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
