当前位置:网站首页>Day 18 of leetcode dynamic planning introduction
Day 18 of leetcode dynamic planning introduction
2022-07-02 16:44:00 【worldinme】
300. The longest increasing subsequence
300. The longest increasing subsequence
Realize the idea :
I use this question O(n^2) Time complexity of completed , It's traversal , It's simple .
Implementation code :
class Solution {
public int lengthOfLIS(int[] nums) {
int len=nums.length;
if(len==1) return 1;
int tempMax=0;
int[] dp=new int[len];
dp[0]=1;
for(int i=1;i<len;i++){
dp[i]=1;
for(int j=0;j<i;j++){
if(nums[i]>nums[j]){
dp[i]=Math.max(dp[j]+1,dp[i]);
}
}
tempMax=Math.max(tempMax,dp[i]);
}
return tempMax;
}
}But then I read the standard solution , Use dichotomy to solve , Feel more clever . The solution is in the link below .
Code attached :
class Solution {
public int lengthOfLIS(int[] nums) {
int[] tail=new int[nums.length];
int ans=0;
for(int num:nums){
int i=0,j=ans;
while(i<j){
int m=(i+j)/2;
if(tail[m]<num) i=m+1;
else j=m;
}
tail[i]=num;
if(ans==j) ans++;
}
return ans;
}
}Realize the idea :

A very simple state transition equation .
Implementation code :
class Solution {
public int wiggleMaxLength(int[] nums) {
int n=nums.length;
int[] up=new int[n];
int[] down=new int[n];
up[0]=1;
down[0]=1;
for(int i=1;i<n;i++){
if(nums[i]>nums[i-1]){
up[i]=Math.max(up[i-1],down[i-1]+1);
down[i]=down[i-1];
}else if(nums[i]<nums[i-1]){
up[i]=up[i-1];
down[i]=Math.max(up[i-1]+1,down[i-1]);
}else{
up[i]=up[i-1];
down[i]=down[i-1];
}
}
return Math.max(up[n-1],down[n-1]);
}
}边栏推荐
- 618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
- 一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
- Download blender on Alibaba cloud image station
- The login box of unity hub becomes too narrow to log in
- False summer vacation
- [fluent] dart data type boolean type (boolean type definition | logical operation)
- LeetCode 4. Find the median (hard) of two positive arrays
- PCL 点云镜像变换
- 大厂面试总结大全
- 数字IC手撕代码--投票表决器
猜你喜欢

Privacy computing technology innovation and industry practice seminar: Learning

LeetCode 1. Sum of two numbers

忆当年高考|成为程序员的你,后悔了吗?

July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance

渗透工具-内网权限维持-Cobalt strike

Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!

Bib | graph representation based on heterogeneous information network learning to predict drug disease association

Typescript array out of order output
![[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)](/img/3f/79dcfcd88d779a5d493b4b539bd448.jpg)
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)

Sim2real environment configuration tutorial
随机推荐
Data security industry series Salon (III) | data security industry standard system construction theme Salon
Typescript array out of order output
数学分析_笔记_第6章:一元函数的Riemann积分
mysql数据库mysqldump为啥没有创建数据库的语句
[fluent] dart data type string type (string definition | string splicing | string API call)
day4
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
618深度复盘:海尔智家的制胜方法论
Take you ten days to easily complete the go micro service series (I)
Routing mode: hash and history mode
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
大廠面試總結大全
Thinking about absolute truth and relative truth
Yyds dry inventory executor package (parameter processing function)
Classic quotations
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
Summary | three coordinate systems in machine vision and their relationships
Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
Kubernetes family container housekeeper pod online Q & A?
TCP congestion control details | 2 background