LC 873. Length of Longest Fibonacci Subsequence

A sequence X_1, X_2, ..., X_n is fibonacci-like if:

  • n >= 3
  • X_i + X_{i+1} = X_{i+2} for all i + 2 <= n

Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A.  If one does not exist, return 0.

(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements.  For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)

Example 1:

Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].

Example 2:

Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].

Note:

  • 3 <= A.length <= 1000
  • 1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
  • (The time limit has been reduced by 50% for submissions in Java, C, and C++.)

my solution.

Runtime: 935 ms, faster than 2.29% of Java online submissions for Length of Longest Fibonacci Subsequence.

我的思路,采用map,虽然也是dp,但是还是一个n2的时间复杂度,看了别人的做法,用的是头尾指针逼近,二分法。今天的周赛里判断数组成三角形的快速判断方法也是这样的。

class Solution {
  public int lenLongestFibSubseq(int[] A) {
    Map<Integer, Map<Integer,Integer>> mp = new HashMap<>();
    int ret = 0;
    for(int i=0; i<A.length; i++){
      if(i == 0) mp.put(A[i], new HashMap<>());
      else {
        mp.put(A[i],new HashMap<>());
        Map<Integer,Integer> mpai = mp.get(A[i]);
        for(int j=0; j<i; j++){
          Map<Integer,Integer> mpaj = mp.get(A[j]);
          mpai.put(A[j], mpaj.getOrDefault(A[i]-A[j],0)+1);
          ret = Math.max(ret, mpai.get(A[j]));
        }
      }
    }
    return ret > 1 ? ret + 1 : 0;
  }
}

网上的思路。

Runtime: 41 ms, faster than 94.29% of Java online submissions for Length of Longest Fibonacci Subsequence.

class Solution {
  public int lenLongestFibSubseq(int[] A) {
    if(A.length < 3) return 0;
    int ret = 0;
    int[][] dp = new int[A.length][A.length];
    for(int i=2; i<A.length; i++){
      int left = 0, right = i-1;
      while(left < right){
        if(A[left] + A[right] == A[i]){
          dp[right][i] = dp[left][right]+1;
          ret = Math.max(ret, dp[right][i]);
          left++;
          right--;
        }else if(A[left] + A[right] > A[i]){
          right--;
        }else {
          left++;
        }
      }
    }
    return ret == 0 ? 0 : ret + 2;
  }
}

原文地址:https://www.cnblogs.com/ethanhong/p/10263323.html

时间: 2024-10-12 19:05:28

LC 873. Length of Longest Fibonacci Subsequence的相关文章

873. Length of Longest Fibonacci Subsequence

A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 <= n Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A.  If on

Length of Longest Fibonacci Subsequence LT873

A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 <= n Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A.  If on

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

300. Longest Increasing Subsequence

Problem statement: Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there

[LeetCode][JavaScript]Longest Increasing Subsequence

Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Not

LeetCode 300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Description Submission Solutions Add to List Total Accepted: 64115 Total Submissions: 170859 Difficulty: Medium Contributors: Admin Given an unsorted array of integers, find the length of longest increasing subsequ

Longest Common Subsequence (DP)

Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Example For "ABCD" and "EDCA", the LCS is "A" (or "D", "C"), return 1. For "ABCD" and &quo

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more

2017-5-14 湘潭市赛 Longest Common Subsequence 想法题

Longest Common Subsequence Accepted : 7 Submit : 66 Time Limit : 3000 MS Memory Limit : 65536 KB Longest Common Subsequence Bobo has a sequence A=(a1,a2,-,an) of length n. He would like to know f(0),f(1),f(2) and f(3) where f(k) denotes the number of