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 ...
随机推荐
- day19——包、logging日志
day19 包 文件夹下具有______init______.py文件就是一个包 方法 import 包.包.包 from 包.包.包 import 模块 需要在______init______.py ...
- Go grpc 与 protobuf
现在很多微服务内部的通信协议都采用rpc,性能高,安全.而grpc则是google退出的rpc plus. protobuf是传输协议,性能高,强大. 来一个server client的通信demo, ...
- Python 基础 while 循环
Python 基础 while 循环 while 循环 在生活中,我们遇到过循环的事情吧?比如循环听歌.在程序中,也是存才的,这就是流程控制语句 while 基本循环 while 条件: # 循环体 ...
- Spring-Boot之Admin服务监控-9
一.Spring Boot Admin用于管理和监控一个或者多个Spring Boot程序.Spring Boot Admin分为Server端和Client 端,Client端可以通过向Http S ...
- jQuery.Form.js使用方法
一.jQuery.Form.js 插件的作用是实现Ajax提交表单. 方法: 1.formSerilize() 用于序列化表单中的数据,并将其自动整理成适合AJAX异步请求的URL地址格式. 2.cl ...
- SQL server 2008数据库的备份与还原(亲测,效果良好)注意采用单用户模式呀
.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Server Management Studio → 数据库:Dsidea ...
- CSS-锚点笔记
注意点: position属性 定义建议元素布局所用的定位机制 {position:static/absolute/relative/fixed;} static:默认值,没有定位 absolute: ...
- Guava Cache用法介绍
背景 缓存的主要作用是暂时在内存中保存业务系统的数据处理结果,并且等待下次访问使用.在日长开发有很多场合,有一些数据量不是很大,不会经常改动,并且访问非常频繁.但是由于受限于硬盘IO的性能或者远程网络 ...
- ar 解压一个.a文件报错: xxx.a is a fat file (use libtool(1) or lipo(1) and ar(1) on it)
Linux 使用终端指令 ar x /Users/apple/Desktop/libWC_LIB_SDKT.a解压一个文件 报错如图所示: 是因为该.a文件包含了多个cpu架构,比如armv7,ar ...
- 爬虫之scrapy框架的crawlspider
一,介绍 CrawlSpider其实是Spider的一个子类,除了继承到Spider的特性和功能外,还派生除了其自己独有的更加强大的特性和功能.其中最显著的功能就是”LinkExtractors链接提 ...