LeetCode 1027. Longest Arithmetic Sequence
原题链接在这里:https://leetcode.com/problems/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:
2 <= A.length <= 2000
0 <= A[i] <= 10000
题解:
State, let dp[diff][i] denotes with diff, up to index i, longest arithmetic length.
For j, check each i<j, with the same diff, dp[diff][j] = dp[diff][i]+1.
Time Complexity: O(n^2). n = A.length.
Space: O(n*(max-min)). max is largest value in A, min is minimum value in A. since count of distinct diff can't be more max-min.
AC Java:
class Solution {
public int longestArithSeqLength(int[] A) {
if(A == null || A.length == 0){
return 0;
} int n = A.length;
int res = 1;
HashMap<Integer, Integer> [] dp = new HashMap[n];
for(int j = 0; j<n; j++){
dp[j] = new HashMap<>();
for(int i = j-1; i>=0; i--){
int d = A[j] - A[i];
int cur = dp[j].getOrDefault(d, 1);
dp[j].put(d, Math.max(cur, dp[i].getOrDefault(d, 1)+1));
res = Math.max(res, dp[j].get(d));
}
} return res;
}
}
LeetCode 1027. Longest Arithmetic Sequence的更多相关文章
- 【leetcode】1027. Longest Arithmetic Sequence
题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...
- [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [Leetcode][JAVA] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- Opencv颜色识别与追踪
这是基于颜色识别的物体追踪 不废话 直接看代码 这是Opencv3的代码 //---------------------------------[头文件.命名空间包含部分]-------------- ...
- C语言 hello
#include <stdio.h> int main() { /* 我的第一个 C 程序 */ printf("Hello, World! \n"); ; } 实例解 ...
- MVC路由规则
1 可以创建多条路由规则,每条路由规则的那么属性不同 2路由规则是有顺序的.如果被前面的规则匹配了,那么后面的规则就没机会了 3 constraints 约束: 4namespaces 命名空间 5r ...
- appium_获取元素状态
元素的属性我们经常会用到,当定位到某个元素后,有时会需要用到这个元素的text值.className.resource-id.checked等. 一般标准的属性我们都可以通过get_attribut ...
- springcolud 的学习(一),架构的发展史
一.传统架构 传统的SSH架构,分为三层架构 web控制层.业务逻辑层.数据库访问层. 传统架构也就是单点应用,就是大家在刚开始初学JavaEE技术的时候SSH架构或者SSM架构,业务没有进行拆分,都 ...
- SpringCloud中服务发现-Eureka
1.Eureka服务端集群开发 1.先创建一个父工程 若是不是普通demo,还有别的配置时,需要注意若是服务开不起来可能就是父类依赖中可能会需要<dependencyManagement> ...
- 安装VMware14可能出现的问题
未能提取文件 安装程序未能提取安装vmware workstation所必须的文件 在没有关闭这个弹框的前提下,Win+R输入%temp%,找到以~setup结尾的文件夹,双击下面的临时文件VMwar ...
- 【转载】C#中使用double.Parse方法将字符串转换为双精度double类型
在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为双精度浮点类型double就是一个常见的类型转换操作,double.Parse方法是C#中专门用来将字符串转换为double ...
- 【转】Vsftpd-3.0.2服务器arm-linux移植—mini2440开发板
Vsftpd-3.0.2服务器arm-linux移植—mini2440开发板 开发板:mini2440(2011.04.21)环境:ubuntu9.10 为方便的将文件上传到开发板,采用vsftpd, ...
- Python,while循环小例子--猜拳游戏(三局二胜)
Python,while循环小例子--猜拳游戏(三局二胜) import random all_choice = ['石头', '剪刀', '布'] prompt = '''(0)石头 (1)剪刀 ( ...