当前位置:网站首页>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]);
}
}边栏推荐
- ⌈ 2022 ⌋ how to use webp gracefully in projects
- 历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...
- Masa framework - DDD design (1)
- Hard core! One configuration center for 8 classes!
- 路由模式:hash和history模式
- PyC file decompile
- Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
- Ranger (I) preliminary perception
- How to use stustr function in Oracle view
- 电脑管理员权限在哪里可以打开
猜你喜欢

Kubernetes three open interfaces first sight

SQL solves the problem of continuous login deformation holiday filtering

Original God 2.6 server download and installation tutorial

Some problems about MySQL installation

Résumé de l'entrevue de Dachang Daquan

Practice of traffic recording and playback in vivo

机器学习-感知机模型

LeetCode 2. Add two numbers

What if the win11 app store cannot load the page? Win11 store cannot load page

How to use stustr function in Oracle view
随机推荐
流批一体在京东的探索与实践
sim2real环境配置教程
虚假的暑假
Yyds dry inventory executor package (parameter processing function)
HMS core machine learning service helps zaful users to shop conveniently
绝对真理和相对真理思考
做机器视觉哪个软件好?
Yolov5 practice: teach object detection by hand
Unity Json 编写
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
Multi task prompt learning: how to train a large language model?
Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
PCL point cloud image transformation
Exploration and practice of integration of streaming and wholesale in jd.com
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
TCP congestion control details | 2 background
数字IC手撕代码--投票表决器
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
路由模式:hash和history模式
System Verilog实现优先级仲裁器