当前位置:网站首页>【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;
}
}
边栏推荐
- 44. Concurrent programming theory
- WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
- BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
- Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
- Ruby replaces gem Alibaba image
- Xctf attack and defense world crypto advanced area best_ rsa
- Micro service knowledge sorting - asynchronous communication technology
- Use unique_ PTR forward declaration? [repetition] - forward declaration with unique_ ptr? [duplicate]
- 2.6 formula calculation
- Global and Chinese market of charity software 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

原生表格-滚动-合并功能

2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程

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

BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu

The 15 year old interviewer will teach you four unique skills that you must pass the interview

Professional interpretation | how to become an SQL developer

第一章: 舍罕王失算

10 smart contract developer tools that miss and lose

Native table - scroll - merge function

Chapter 1: extend the same code decimal sum s (D, n)
随机推荐
6. Data agent object Defineproperty method
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
PR 2021 quick start tutorial, material import and management
44. Concurrent programming theory
How to improve data security by renting servers in Hong Kong
Acquisition and transmission of parameters in automatic testing of JMeter interface
04 -- QT OpenGL two sets of shaders draw two triangles
Global and Chinese markets of lithium chloride 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
05 -- QT OpenGL draw cube uniform
February 14-20, 2022 (osgear source code debugging +ue4 video +ogremain source code transcription)
Popularize the basics of IP routing
Chapter 1: recursively find the factorial n of n!
Win10 share you don't have permission
10 smart contract developer tools that miss and lose
Rd file name conflict when extending a S4 method of some other package
1.4 learn more about functions
Microservice framework - frequently asked questions
Parental delegation mechanism
CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析