当前位置:网站首页>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]);
}
}边栏推荐
- Bib | graph representation based on heterogeneous information network learning to predict drug disease association
- Multi task prompt learning: how to train a large language model?
- Yyds dry inventory executor package (parameter processing function)
- Unity Json 编写
- LeetCode 2. Add two numbers
- Does bone conduction earphone have external sound? Advantages of bone conduction earphones
- Sim2real environment configuration tutorial
- July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
- Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
- 忆当年高考|成为程序员的你,后悔了吗?
猜你喜欢

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

Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)

Kubernetes three open interfaces first sight

Hard core! One configuration center for 8 classes!

LeetCode 2. Add two numbers

Penetration tool - intranet permission maintenance -cobalt strike

Mysql database mysqldump why there is no statement to create a database

Machine learning perceptron model

Seal Library - installation and introduction
![OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation](/img/8b/36be3191a7d71f4a8c8181eaed8417.jpg)
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
随机推荐
How to solve the failure of printer driver installation of computer equipment
618深度複盤:海爾智家的制勝方法論
Sim2real environment configuration tutorial
路由模式:hash和history模式
MySQL port
Student course selection system (curriculum design of Shandong Agricultural University)
Yyds dry inventory company stipulates that all interfaces use post requests. Why?
How to choose the right kubernetes storage plug-in? (09)
Remove the underline in router link
Exploration and practice of integration of streaming and wholesale in jd.com
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
[fluent] dart data type string type (string definition | string splicing | string API call)
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
What if the win11 app store cannot load the page? Win11 store cannot load page
Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
Download blender on Alibaba cloud image station
HMS core machine learning service helps zaful users to shop conveniently
unity Hub 登錄框變得很窄 無法登錄
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
做机器视觉哪个软件好?