当前位置:网站首页>【leetcode】1027. Longest arithmetic sequence (dynamic programming)
【leetcode】1027. Longest arithmetic sequence (dynamic programming)
2022-07-03 20:02:00 【Chinese fir sauce_】
subject :
1027. The longest arithmetic sequence
Give you an array of integers nums, return nums The length of the longest isochromatic subsequence in .
Think about it ,nums The subsequence of is a list nums[i1], nums[i2], ..., nums[ik] , And 0 <= i1 < i2 < ... < ik <= nums.length - 1. And if the seq[i+1] - seq[i]( 0 <= i < seq.length - 1) It's all the same , So sequence seq It's equal difference .
Example 1:
Input :nums = [3,6,9,12]
Output :4
explain :
The whole array has a tolerance of 3 Equal difference sequence of .
Example 2:
Input :nums = [9,4,7,2,10]
Output :3
explain :
The longest isochromatic subsequence is [4,7,10].
Example 3:
Input :nums = [20,1,15,3,10,5,8]
Output :4
explain :
The longest isochromatic subsequence is [20,15,10,5].
Tips :
2 <= nums.length <= 1000
0 <= nums[i] <= 500
Dynamic programming :
Ideas :
1. Definition DP Array
dp[i][d]: It means the first one i Number , The difference from the previous number is d when , The length of the longest arithmetic sequence that can be formed .
We can get the equation of state transition :dp[i][d] = max(dp[i][d],dp[j][d]+1), among 0<=j<i.
2. Element difference d It means
Because the array element size is 0<=nums[i]<=500, So in the array The range of the difference between the two elements is -500<= d<=500.
therefore , have access to d+500 Map the difference to [0,1000] It is convenient for us to define dp Array .
- Time complexity :O(n^{2})
- Spatial complexity :O(n*d)
class Solution {
public int longestArithSeqLength(int[] nums) {
int len = nums.length;
if(len == 0) return 0;
int res = 0;
int[][]dp = new int[len][1001];
for(int i = 0; i < len;i++){
for(int j = 0; j < i;j++){
// nums[i] Make a difference with all the previous numbers
int d = nums[i] - nums[j] + 500;
dp[i][d] = Math.max(dp[i][d],dp[j][d] + 1);
res = Math.max(res,dp[i][d]);
}
}
// On initialization int[][] dp = new int[n][1010]; The default value of the element is 0, The first element of the arithmetical sequence is not included . Therefore return res+1.
// If it is initialized to 1, Return directly later ans
return res + 1;
}
}
边栏推荐
- 7. Data broker presentation
- Typora charges, WTF? Still need support
- Implementation of stack
- Global and Chinese market of rubidium standard 2022-2028: Research Report on technology, participants, trends, market size and share
- FPGA 学习笔记:Vivado 2019.1 工程创建
- PR 2021 quick start tutorial, material import and management
- February 14-20, 2022 (osgear source code debugging +ue4 video +ogremain source code transcription)
- 2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
- Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
- MPLS configuration
猜你喜欢

2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】

Chapter 1: recursively find the factorial n of n!

2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
![2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]](/img/b6/5d6b946d8001e2d73c2cadbdce72fc.png)
2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]

2.5 conversion of different data types (2)

Chapter 1: sum of three factorials, graph point scanning

PR 2021 quick start tutorial, how to create a new sequence and set parameters?

BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply

BOC protected tryptophan zinc porphyrin (Zn · TAPP Trp BOC) / copper porphyrin (Cu · TAPP Trp BOC) / cobalt porphyrin (cobalt · TAPP Trp BOC) / iron porphyrin (Fe · TAPP Trp BOC) / Qiyue supply

Virtual machine installation deepin system
随机推荐
Kubernetes cluster builds efk log collection platform
Global and Chinese market of rubidium standard 2022-2028: Research Report on technology, participants, trends, market size and share
Derivation of decision tree theory
HCIA-USG Security Policy
2022-06-28 网工进阶(十三)IS-IS-路由过滤、路由汇总、认证、影响ISIS邻居关系建立的因素、其他命令和特性
2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration
第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
PR 2021 quick start tutorial, how to create a new sequence and set parameters?
Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
About unregistered transfer login page
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
2.2 integer
Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
Rad+xray vulnerability scanning tool
BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
Bool blind note - score query
Global and Chinese markets of lithium chloride 2022-2028: Research Report on technology, participants, trends, market size and share
How to improve data security by renting servers in Hong Kong
Chapitre 1: le roi de shehan a mal calculé
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)