当前位置:网站首页>【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;
}
}
边栏推荐
- Parental delegation mechanism
- 2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
- 2.1 use of variables
- NFT without IPFs and completely on the chain?
- Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
- [Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
- Global and Chinese market of charity software 2022-2028: Research Report on technology, participants, trends, market size and share
- Basic command of IP address configuration ---ip V4
- AcWing 1460. Where am i?
- Micro service knowledge sorting - asynchronous communication technology
猜你喜欢

MPLS configuration

04 -- QT OpenGL two sets of shaders draw two triangles

Xctf attack and defense world crypto master advanced area olddriver

2.6 formula calculation

2022-06-25 advanced network engineering (XI) IS-IS synchronization process of three tables (neighbor table, routing table, link state database table), LSP, cSNP, psnp, LSP

Phpstudy set LAN access

Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla

Kubernetes cluster builds efk log collection platform

Professional interpretation | how to become an SQL developer

Virtual machine installation deepin system
随机推荐
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
Upgrade PIP and install Libraries
Native table - scroll - merge function
QT tutorial: signal and slot mechanism
Rd file name conflict when extending a S4 method of some other package
unittest框架基本使用
PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share
2022-06-30 网工进阶(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
PR notes:
Chapter 1: extend the same code decimal sum s (D, n)
7. Data broker presentation
Day11 - my page, user information acquisition, modification and channel interface
Pat grade B 1009 is ironic (20 points)
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
Global and Chinese market of speed limiter 2022-2028: Research Report on technology, participants, trends, market size and share
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
Acquisition and transmission of parameters in automatic testing of JMeter interface
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
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