当前位置:网站首页>Leetcode 300: longest increasing subsequence
Leetcode 300: longest increasing subsequence
2022-07-19 16:23:00 【Swarford】
Ideas : Dynamic programming (dp table)
The kernel of dynamic programming ⼼ The design idea is mathematical induction ;
If you want to prove ⼀ A mathematical conclusion , Then we Let's assume that the conclusion is k < n Shi Cheng ⽴, Then based on this assumption , Try to deduce and prove k = n This conclusion also becomes ⽴. Such as The result can be proved , So it shows that this conclusion is right for k Equal to any number ⽴.
Design dynamic programming algorithm , It's not necessary ⼀ individual dp Array ? We can assume that dp[0…i-1] Have been calculated 了 , Then ask ⾃⼰: How to work out... From these results dp[i]?
dp Definition of array :dp[i] Said to nums[i] The longest increment at the end of this number ⼦ The length of the sequence ;
base case:dp[i] The initial value is 1, Because in order to nums[i] The end of the most ⻓ Increasing ⼦ Sequence start The code should include it ⾃⼰.

According to this definition , Our end result (⼦ The most ⼤⻓ degree ) Should be dp Most in array ⼤ value :

Be careful : Not at all i The bigger it is , The longer the subsequence is !
Suppose you already know dp[0…4] All the results of , How to deduce from these known results dp[5] ?
Such as :
nums[5] = 3, Since it is looking for incremental ⼦ Sequence , Just find the ending in front of you 3 Small subsequences , And then put 3 To the end of these subsequences , Can form ⼀ A new increasing subsequence ,⽽ And the length of this new subsequence plus ⼀.
nums[5] front ⾯ Which elements are smaller than nums[5]? use for Cycle comparison ⼀ Waves can find these elements .
The most... That ends with these elements ⻓ Increasing ⼦ Sequential ⻓ What is the degree ? review ⼀ Next we are right dp Definition of array , It records exactly the most ⻓ Increasing ⼦ Sequential ⻓ degree .
In the example nums[0] and nums[4] All are less than nums[5] Of , Then contrast dp[0] and dp[4] Value , We Give Way nums[5] And longer increments ⼦ Sequence binding , obtain dp[5] = 2+1=3 ;

summary :
1. clear dp Definition of array . this ⼀ Step is important for any dynamic programming problem , If not, or not clear enough , It will hinder the next step .
2. according to dp Definition of array , shipment ⽤ The idea of mathematical induction , hypothesis dp[0…i-1] All known , Find a way to find out dp[i],⼀ Dan this ⼀ Step through , The whole problem was basically solved .
Java Realization :
class Solution {
public int lengthOfLIS(int[] nums) {
// An element corresponds to a subsequence length , So there's no need to add 1
int dp[]=new int[nums.length];
Arrays.fill(dp,1); // Ask for the biggest , Initialize to small
for(int i=0;i<nums.length;i++){
// Traverse each element in turn as the end
for(int j=0;j<i;j++){
// Traverse all elements before the end element
// Traverse all of Smaller than the end element j , And choose all j The longest neutron sequence , add 1 It's the result !
if(nums[i]>nums[j]){
dp[i]=Math.max(dp[i],dp[j]+1);
}
}
}
int res=-1;
for(int i=0;i<nums.length;i++){
res=Math.max(res,dp[i]);
}
return res;
}
}
边栏推荐
猜你喜欢

The read image of OpenCV notes is de distorted and saved as a new image

Mysql如何实现不存在则插入,存在则更新

CH549/CH548学习笔记1 - 硬件设计

MySQL - 表字段的唯一键约束

对象内存布局和synchronized锁升级

Ch549/ch548 learning notes 2 - system clock

Ch549/ch548 learning notes 1 - hardware design

Liquibase learning 3 - loglevel log level, common commands

基于二叉树的传球问题思考与推测

各版本VOS服务的停止、启动和重启命令详解
随机推荐
【单片机仿真项目】数码管依次显示 0 到 5(proteus原理图+keil代码)
Is it free to open an account online with flush software for stock speculation? Is it safe to open an account?
Is it free to open an account online with flush software? Is it safe to open an account?
力扣 565. 数组嵌套
Opencv image transparent area clipping imagecroppingtrn
Ch549/ch548 learning notes 8 - USB device interrupt processing
深度学习环境配置Pytorch
PwnTheBox,Web:Double-S
Ugui source code analysis - clipperregistry
PwnTheBox,Web:Double-S
HCIA -- OSPF experimental report
Mysql如何实现不存在则插入,存在则更新
线上基金开户是否安全呢?在线求答案
MySQL -调整列的约束
腾讯出品的良心产品?翻译君功能详细体验
Compile error recipe terminated with error when referencing latex pictures under vscade Retry building the project.
【單片機仿真項目】報警燈(proteus原理圖+keil代碼)
手写简易Promise代码注释
A Ye's new hairstyle
Definition and usage of callback function and qsort function in C language
