【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 arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).

Example 1:

Input: [3,6,9,12]
Output: 4
Explanation:
The whole array is an arithmetic sequence with steps of length = 3.

Example 2:

Input: [9,4,7,2,10]
Output: 3
Explanation:
The longest arithmetic subsequence is [4,7,10].

Example 3:

Input: [20,1,15,3,10,5,8]
Output: 4
Explanation:
The longest arithmetic subsequence is [20,15,10,5].

Note:

  1. 2 <= A.length <= 2000
  2. 0 <= A[i] <= 10000

解题思路:首先用字典记录A中每个元素出现的下标,接下来求出任意A[i]与A[j]的差值d,依次判断A[j] += d是否存在于A中,并且要求A[j] + d的下标的最小值要大于j,最终即可求出最长的等差数列。

代码如下:

class Solution(object):
    def longestArithSeqLength(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        import bisect
        res = 0
        dic = {}
        for i,v in enumerate(A):
            dic[v] = dic.setdefault(v,[]) + [i]
        for i in range(len(A)):
            for j in range(i+1,len(A)):
                count = 2
                diff = A[j] - A[i]
                next = A[j] + diff
                smallestInx = j
                while True:
                    if next not in dic:
                        break
                    inx = bisect.bisect_right(dic[next],smallestInx)
                    if inx == len(dic[next]):
                        break
                    smallestInx = dic[next][inx]
                    next = next + diff
                    count += 1
                res = max(res,count)
        return res

原文地址:https://www.cnblogs.com/seyjs/p/10765630.html

时间: 2024-10-07 10:16:09

【leetcode】1027. Longest Arithmetic Sequence的相关文章

【Leetcode】128. 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

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>

[动态规划] 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】014 Longest Common Prefix

题目:LeetCode 014 Longest Common Prefix 题意:给出一组字符串求公共前缀 思路:很多个字符串的公共前缀应该不会很高,所以直接暴力解决就好 但是又有个特判,即当只有一个字符串的时候,直接返回即可.另外,一定要注意每次利用下标访问字符串的时候,一定要判断是否在有效范围内. 代码如下: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { 4 int

【LeetCode】003 Longest Substring Without Repeating Characters

题目:LeetCode 003 Longest Substring Without Repeating Characters 题意:给一个字符串,找到一个没有重复字符的最长子串. 样例:”bbbbb” 返回”b”的长度1:”abcabcbb”返回”abc”的长度3. 思路: 动态规划.dp[i]表示以第i个字符为结尾的无重复字符的子串的最长的长度,需要一个辅助数组idx[s[i]]记录字符当前字符s[i]上一次出现的位置,如果未出现过则为-1.所以得到如下的递推公式: 另外由于不确定字符串长度的

【LeetCode】005 Longest Palindromic Substring

题目:LeetCode 005 Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 题意:字符串S,找到S中的最长回文子串.假定S最长为100

【LeetCode】14 - Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. Solution: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { //runtime:4ms 4 string str=""; 5 if(strs.empty())return

【LeetCode】5. Longest Palindromic Substring 最大回文子串

题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路:用Manacher算法得到最大回文子串的长度,得到带#的最大回文子串,用split剔除#后转化成String形式输出即可. public

【leetcode】5. Longest Palindromic Substring

题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 解题分析: 之前百度实习生面试也被问到这个问题.这个题比较简单.就是在分别考虑对称字符字串为奇数和偶数的情况,在不超过字符串范围的情况下向