LeetCode 1027. Longest Arithmetic Sequence

dp问题,一维是没法dp的,因为我们还要记录一个diff才能判断是否是Arithmetic Sequence。

dp[i][diff] 表示以 A[i] 结尾,等差为 diff 的最大长度。从这种角度来看本题和 LeetCode 300. Longest Increasing Subsequence / 354. Russian Doll Envelopes 极为相似。

class Solution {
public:
    int longestArithSeqLength(vector<int>& A) {
        int n=A.size();
        if (n<=1) return n;
        vector<unordered_map<int,int>> dp(n);
        int res=0;
        for (int i=0;i<n;++i){
            for (int j=0;j<i;++j){
                int diff=A[i]-A[j];
                if (dp[j].count(diff))
                    dp[i][diff] = max(dp[i][diff], dp[j][diff]+1);
                else
                    dp[i][diff] = 2;
                res = max(res,dp[i][diff]);
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/hankunyan/p/11456719.html

时间: 2024-11-05 20:47:59

LeetCode 1027. Longest Arithmetic Sequence的相关文章

[动态规划] leetcode 1027 Longest Arithmetic Sequence

problem:https://leetcode.com/problems/longest-arithmetic-sequence/description/ 最长子序列类型问题.因为状态比较多,可以存在hash表里,之后直接查找. class Solution { public: int longestArithSeqLength(vector<int>& A) { vector<unordered_map<int,int>> dp(A.size()); int

【leetcode】1027. Longest Arithmetic Sequence

题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

【LeetCode】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al

[LeetCode][JavaScript]Longest Consecutive Sequence

Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

[Lintcode]124. Longest Consecutive Sequence/[Leetcode]128. Longest Consecutive Sequence

124. Longest Consecutive Sequence/128. Longest Consecutive Sequence 本题难度: Medium/Hard Topic: Data Structure Description Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Example Given [100, 4, 200, 1,

Leetcode 贪心 Longest Consecutive Sequence

Longest Consecutive Sequence Total Accepted: 19169 Total Submissions: 68303My Submissions Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consec

【LeetCode】Longest Consecutive Sequence 解题报告

[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should