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 ...
随机推荐
- golang(一)
开篇先来个Go语言的吉祥物-金花鼠Gordon. golang是谷歌2009年发布的开源编程语言,截止目前go的release版本已经到了1.10.go语言的开发人员都是计算机界大神一般的存在: Th ...
- python使用自带模块httplib进行http请求
#-*- encoding:utf-8 -*- import httplib, time class httpRequest(): def __init__(self, headers, reques ...
- python_协程
协程 问题一: 生成器与函数的区别?生成器分阶段的返回多个值,相当于有多个出口(结果): yield ''' yield # 中断.返回函数值 1.只能在函数中使用 2.会暂停函数执行并且返回表达式结 ...
- 【题解】Luogu P5342 [TJOI2019]甲苯先生的线段树
原题传送门 挺有趣的一道题 \(c=1\),暴力求出点权和n即可 \(c=2\),先像\(c=1\)一样暴力求出点权和n,考虑有多少路径点权和也为n 考虑设x为路径的转折点,\(L\)为\(x\)向左 ...
- 开启Telnet服务
在Win7系统中安装和启动Telnet服务非常简单:依次点击“开始”→“控制面板”→“程序”,“在程序和功能”找到并点击“打开或关闭Windows功能”进入Windows 功能设置对话框.找到并勾选“ ...
- SP375 QTREE - Query on a tree (树剖)
题目 SP375 QTREE - Query on a tree 解析 也就是个蓝题,因为比较长 树剖裸题(基本上),单点修改,链上查询. 顺便来说一下链上操作时如何将边上的操作转化为点上的操作: 可 ...
- js事件(十二)
一.事件三要素1.事件目标[谁触发的该事件(引起该事件触发的源头:target)]2.事件处理程序[处理相应事件的函数]3.事件对象[触发事件产生的携带事件信息的对象] 二.事件流[从页面中接受事件的 ...
- 多线程:Monitor、synchronized、volatile
Moniter的实现原理 再有人问你synchronized是什么,就把这篇文章发给他 深入理解Java中的volatile关键字 既生synchronized,何生volatile
- tcp、udp协议栈
tcp struct tcphdr { __be16 source; //源端口 __be16 dest; //目的端口 __be32 seq; //序列号 __be32 ack_seq; //确认号 ...
- MySQL DataType--定点数(Fixed-Point Types)学习
DECIMAL和NUMERIC MySQL支持两种定点数类型:DECIMAL和NUMERIC,而NUMERIC实现为DECIMAL,因此MySQL中DECIMAL和NUMERIC等价相同. 如使用下面 ...