当前位置:网站首页>【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;
}
}
边栏推荐
- Chapitre 1: le roi de shehan a mal calculé
- Basic command of IP address configuration ---ip V4
- What is the difference between a kill process and a close process- What are the differences between kill process and close process?
- Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
- Find a line in a file and remove it
- Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
- 6. Data agent object Defineproperty method
- Today's work summary and plan: February 14, 2022
- Microservice knowledge sorting - search technology and automatic deployment technology
- Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
猜你喜欢

Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!

Wechat applet quick start (including NPM package use and mobx status management)

Chapitre 1: le roi de shehan a mal calculé

Native table - scroll - merge function

Chapter 1: seek common? Decimal and S (D, n)

Point cloud data denoising

Virtual machine installation deepin system

Phpstudy set LAN access

2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)

PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
随机推荐
Global and Chinese market of speed limiter 2022-2028: Research Report on technology, participants, trends, market size and share
47. Process lock & process pool & Collaboration
FPGA 学习笔记:Vivado 2019.1 工程创建
10 smart contract developer tools that miss and lose
February 14-20, 2022 (osgear source code debugging +ue4 video +ogremain source code transcription)
Chapter 2: 4-digit Kaplan number, search even digit Kaplan number, search n-digit 2-segment sum square number, m-digit ingenious square number without 0, specify the number to form a 7-digit square nu
2.5 conversion of different data types (2)
1.5 learn to find mistakes first
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
Make a simple text logo with DW
04 -- QT OpenGL two sets of shaders draw two triangles
Promethus
Chapter 1: seek common? Decimal and S (D, n)
How to improve data security by renting servers in Hong Kong
[raid] [simple DP] mine excavation
WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
第一章: 舍罕王失算
Parental delegation mechanism
Part 27 supplement (27) buttons of QML basic elements