当前位置:网站首页>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]);
}
}边栏推荐
- How to solve the failure of printer driver installation of computer equipment
- HMS core machine learning service helps zaful users to shop conveniently
- Unity Json 编写
- Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
- ⌈ 2022 ⌋ how to use webp gracefully in projects
- Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
- Classic quotations
- What is normal distribution? What is the 28 law?
- PWM控制舵机
- LeetCode 2. 两数相加
猜你喜欢

数学分析_笔记_第6章:一元函数的Riemann积分

数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙

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

The login box of unity hub becomes too narrow to log in

SSM整合-异常处理器及项目异常处理方案

LeetCode 1. 两数之和

Remove the underline in router link

机器学习-感知机模型

How to solve the failure of printer driver installation of computer equipment

Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
随机推荐
小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
Some problems about MySQL installation
Routing mode: hash and history mode
Recalling the college entrance examination and becoming a programmer, do you regret it?
月报总结|Moonbeam6月份大事一览
渗透工具-内网权限维持-Cobalt strike
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
618深度复盘:海尔智家的制胜方法论
[fluent] dart data type boolean type (boolean type definition | logical operation)
流批一体在京东的探索与实践
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Yolov5 practice: teach object detection by hand
数学分析_笔记_第6章:一元函数的Riemann积分
Set the background picture in the idea (ultra detailed)
图书管理系统(山东农业大学课程设计)
Data security industry series Salon (III) | data security industry standard system construction theme Salon
AcWing 300. Task arrangement
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
大厂面试总结大全
TCP拥塞控制详解 | 2. 背景