< Summary

Information
Class: LeetCode.Algorithms.LengthOfLongestFibonacciSubsequence.LengthOfLongestFibonacciSubsequenceDynamicProgramming
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LengthOfLongestFibonacciSubsequence\LengthOfLongestFibonacciSubsequenceDynamicProgramming.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 57
Line coverage: 100%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LenLongestFibSubseq(...)91.66%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LengthOfLongestFibonacciSubsequence\LengthOfLongestFibonacciSubsequenceDynamicProgramming.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.LengthOfLongestFibonacciSubsequence;
 13
 14/// <inheritdoc />
 15public class LengthOfLongestFibonacciSubsequenceDynamicProgramming : ILengthOfLongestFibonacciSubsequence
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2)
 19    ///     Space complexity - O(n^2)
 20    /// </summary>
 21    /// <param name="arr"></param>
 22    /// <returns></returns>
 23    public int LenLongestFibSubseq(int[] arr)
 224    {
 225        var dictionary = new Dictionary<int, int>();
 26
 3427        for (var i = 0; i < arr.Length; i++)
 1528        {
 1529            dictionary[arr[i]] = i;
 1530        }
 31
 232        var dp = new int[arr.Length, arr.Length];
 33
 234        var result = 0;
 35
 3436        for (var i = 0; i < arr.Length; i++)
 1537        {
 12838            for (var j = i + 1; j < arr.Length; j++)
 4939            {
 4940                dp[i, j] = 2;
 41
 4942                var potentialPrev = arr[j] - arr[i];
 43
 4944                if (!dictionary.TryGetValue(potentialPrev, out var k) || k >= i)
 3445                {
 3446                    continue;
 47                }
 48
 1549                dp[i, j] = dp[k, i] + 1;
 50
 1551                result = Math.Max(result, dp[i, j]);
 1552            }
 1553        }
 54
 255        return result >= 3 ? result : 0;
 256    }
 57}