Skip to content

Latest commit

 

History

History
231 lines (193 loc) · 6.41 KB

File metadata and controls

231 lines (193 loc) · 6.41 KB

English Version

题目描述

如果序列 X_1, X_2, ..., X_n 满足下列条件,就说它是 斐波那契式 的:

  • n >= 3
  • 对于所有 i + 2 <= n,都有 X_i + X_{i+1} = X_{i+2}

给定一个严格递增的正整数数组形成序列 arr ,找到 arr 中最长的斐波那契式的子序列的长度。如果一个不存在,返回  0 。

(回想一下,子序列是从原序列 arr 中派生出来的,它从 arr 中删掉任意数量的元素(也可以不删),而不改变其余元素的顺序。例如, [3, 5, 8] 是 [3, 4, 5, 6, 7, 8] 的一个子序列)

 

示例 1:

输入: arr = [1,2,3,4,5,6,7,8]
输出: 5
解释: 最长的斐波那契式子序列为 [1,2,3,5,8] 。

示例 2:

输入: arr = [1,3,7,11,12,14,18]
输出: 3
解释: 最长的斐波那契式子序列有 [1,11,12]、[3,11,14] 以及 [7,11,18] 。

 

提示:

  • 3 <= arr.length <= 1000
  • 1 <= arr[i] < arr[i + 1] <= 10^9

解法

方法一:动态规划

  • 状态表示:dp[j][i] 表示斐波那契式最后两项为 arr[j], arr[i] 时的最大子序列长度。
  • 状态计算:dp[j][i] = dp[k][j] + 1(当且仅当 k < j < i,并且 arr[k] + arr[j] == arr[i]), ans = max(ans, dp[j][i])

Python3

class Solution:
    def lenLongestFibSubseq(self, arr: List[int]) -> int:
        mp = {v: i for i, v in enumerate(arr)}
        n = len(arr)
        dp = [[0] * n for _ in range(n)]
        for i in range(n):
            for j in range(i):
                dp[j][i] = 2
        ans = 0
        for i in range(n):
            for j in range(i):
                d = arr[i] - arr[j]
                if d in mp and (k := mp[d]) < j:
                    dp[j][i] = max(dp[j][i], dp[k][j] + 1)
                    ans = max(ans, dp[j][i])
        return ans

Java

class Solution {
    public int lenLongestFibSubseq(int[] arr) {
        int n = arr.length;
        Map<Integer, Integer> mp = new HashMap<>();
        for (int i = 0; i < n; ++i) {
            mp.put(arr[i], i);
        }
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                dp[j][i] = 2;
            }
        }
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                int d = arr[i] - arr[j];
                if (mp.containsKey(d)) {
                    int k = mp.get(d);
                    if (k < j) {
                        dp[j][i] = Math.max(dp[j][i], dp[k][j] + 1);
                        ans = Math.max(ans, dp[j][i]);
                    }
                }
            }
        }
        return ans;
    }
}

C++

class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        unordered_map<int, int> mp;
        int n = arr.size();
        for (int i = 0; i < n; ++i) mp[arr[i]] = i;
        vector<vector<int>> dp(n, vector<int>(n));
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < i; ++j)
                dp[j][i] = 2;
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                int d = arr[i] - arr[j];
                if (mp.count(d)) {
                    int k = mp[d];
                    if (k < j) {
                        dp[j][i] = max(dp[j][i], dp[k][j] + 1);
                        ans = max(ans, dp[j][i]);
                    }
                }
            }
        }
        return ans;
    }
};

Go

func lenLongestFibSubseq(arr []int) int {
	n := len(arr)
	mp := make(map[int]int, n)
	for i, v := range arr {
		mp[v] = i + 1
	}
	dp := make([][]int, n)
	for i := 0; i < n; i++ {
		dp[i] = make([]int, n)
		for j := 0; j < i; j++ {
			dp[j][i] = 2
		}
	}
	ans := 0
	for i := 0; i < n; i++ {
		for j := 0; j < i; j++ {
			d := arr[i] - arr[j]
			k := mp[d] - 1
			if k >= 0 && k < j {
				dp[j][i] = max(dp[j][i], dp[k][j]+1)
				ans = max(ans, dp[j][i])
			}
		}
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

JavaScript

/**
 * @param {number[]} arr
 * @return {number}
 */
var lenLongestFibSubseq = function (arr) {
    const mp = new Map();
    const n = arr.length;
    const dp = new Array(n).fill(0).map(() => new Array(n).fill(0));
    for (let i = 0; i < n; ++i) {
        mp.set(arr[i], i);
        for (let j = 0; j < i; ++j) {
            dp[j][i] = 2;
        }
    }
    let ans = 0;
    for (let i = 0; i < n; ++i) {
        for (let j = 0; j < i; ++j) {
            const d = arr[i] - arr[j];
            if (mp.has(d)) {
                const k = mp.get(d);
                if (k < j) {
                    dp[j][i] = Math.max(dp[j][i], dp[k][j] + 1);
                    ans = Math.max(ans, dp[j][i]);
                }
            }
        }
    }
    return ans;
};

...