-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution873.cs
42 lines (35 loc) · 1.02 KB
/
Solution873.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution873
{
/// <summary>
/// 873. Length of Longest Fibonacci Subsequence - Medium
/// <a href="https://leetcode.com/problems/length-of-longest-fibonacci-subsequence">See the problem</a>
/// </summary>
public int LenLongestFibSubseq(int[] arr)
{
var n = arr.Length;
var set = new HashSet<int>(arr);
var dp = new Dictionary<(int, int), int>();
int result = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
int a = arr[i];
int b = arr[j];
int count = 2;
while (set.Contains(a + b))
{
count++;
int temp = a;
a = b;
b = temp + b;
}
result = Math.Max(result, count);
}
}
return result > 2 ? result : 0;
}
}