当前位置:网站首页>【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;
}
}
边栏推荐
- Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
- 4. Data binding
- Today's work summary and plan: February 14, 2022
- Xctf attack and defense world crypto master advanced area olddriver
- Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
- Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share
- 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
- Use of aggregate functions
- Class loading process
- 5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
猜你喜欢
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
IPv6 experiment
Xctf attack and defense world crypto master advanced area olddriver
2022-06-30 网工进阶(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue
Derivation of decision tree theory
原生表格-滚动-合并功能
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
BOC protected amino acid porphyrins TAPP ala BOC, TAPP Phe BOC, TAPP Trp BOC, Zn · TAPP ala BOC, Zn · TAPP Phe BOC, Zn · TAPP Trp BOC Qiyue
随机推荐
Promethus
Global and Chinese market of rubidium standard 2022-2028: Research Report on technology, participants, trends, market size and share
5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
Chapter 1: extend the same code decimal sum s (D, n)
Rad+xray vulnerability scanning tool
NFT without IPFs and completely on the chain?
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
Chapitre 1: le roi de shehan a mal calculé
1.4 learn more about functions
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
unittest框架基本使用
P5.js development - setting
The 15 year old interviewer will teach you four unique skills that you must pass the interview
Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
Change deepin to Alibaba image source
PR notes:
Find a line in a file and remove it
Chapter 1: recursively find the factorial n of n!
Professional interpretation | how to become an SQL developer
FAQs for datawhale learning!