当前位置:网站首页>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]);
}
}边栏推荐
- [Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology
- 数学分析_笔记_第5章:一元微分学
- 渗透工具-内网权限维持-Cobalt strike
- Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
- Mysql database mysqldump why there is no statement to create a database
- PyC file decompile
- SQL solves the problem of continuous login deformation holiday filtering
- 分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
- Interview summary of large factories
- Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Data security industry series Salon (III) | data security industry standard system construction theme Salon

Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification

Download blender on Alibaba cloud image station

大廠面試總結大全

做机器视觉哪个软件好?

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

Seal Library - installation and introduction

PWM控制舵机

Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
![[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array](/img/51/f9c1eed37794db8c8d0eefd60b9e3d.jpg)
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
随机推荐
[fluent] dart data type boolean type (boolean type definition | logical operation)
Summary | three coordinate systems in machine vision and their relationships
618深度複盤:海爾智家的制勝方法論
unity Hub 登錄框變得很窄 無法登錄
ROW_ NUMBER()、RANK()、DENSE_ Rank difference
【云原生】简单谈谈海量数据采集组件Flume的理解
关于mysql安装的一些问题
数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙
路由模式:hash和history模式
Download blender on Alibaba cloud image station
pwm呼吸灯
LeetCode 5. Longest Palindromic Substring
数学分析_笔记_第5章:一元微分学
外企高管、连续创业者、瑜伽和滑雪高手,持续迭代重构的程序人生
Hard core! One configuration center for 8 classes!
TCP congestion control details | 2 background
LeetCode 1. 两数之和
忆当年高考|成为程序员的你,后悔了吗?
Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
Global and Chinese markets for airport baggage claim conveyors 2022-2028: technology, participants, trends, market size and share Research Report