当前位置:网站首页>300. Longest increasing subsequence
300. Longest increasing subsequence
2022-07-25 05:06:00 【rananie】
List of articles
300. The longest increasing subsequence
subject
Give you an array of integers nums , Find the length of the longest strictly increasing subsequence .
Subsequence Is a sequence derived from an array , Delete ( Or do not delete ) Elements in an array without changing the order of the rest . for example ,[3,6,2,7] It's an array [0,3,1,6,2,2,7] The subsequence .
Example 1:
Input :nums = [10,9,2,5,3,7,101,18]
Output :4
explain : The longest increasing subsequence is [2,3,7,101], So the length is 4 .
Example 2:
Input :nums = [0,1,0,3,2,3]
Output :4
Example 3:
Input :nums = [7,7,7,7,7,7,7]
Output :1
Tips :
1 <= nums.length <= 2500
-104 <= nums[i] <= 104
Advanced :
You can reduce the time complexity of the algorithm to O(n log(n)) Do you ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/longest-increasing-subsequence
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Answer key
Multistage decision making + Optimal solution , Try dynamic programming .
determine dp The meaning of array and its subscript
dp[i]: With nums[i] The length of the strictly increasing subsequence at the end is dp[i]
dp Array recursion
dp[i] The value of is in nums[i] The length of the strictly increasing subsequence at the end , So we need to i-1 Start looking forward nums[i] Small number nums[j], Than nums[i] There must be many small numbers , choice dp[j] The biggest one nums[j] Form a new strictly increasing subsequence .
If you don't find it, just nums[i] Start with a new strictly increasing subsequence .
for(let i=0;i<nums.length;i++){
for(let j=i;j>=0;j--){
if(nums[j]<nums[i])dp[i] = Math. max(dp[i],dp[j]+1);
}
res = Math.max(res,dp[i]);
}
dp Array initialization
dp The array is initialized to 1, It means to start with oneself 、 The length of the strictly increasing subsequence at its end is 1
let dp = new Array(nums.length).fill(1) ;
Code
var lengthOfLIS = function(nums) {
let dp = new Array(nums.length).fill(1) ;
let res = 1;
for(let i=0;i<nums.length;i++){
for(let j=i;j>=0;j--){
if(nums[j]<nums[i])dp[i] = Math. max(dp[i],dp[j]+1);
}
res = Math.max(res,dp[i]);
}
return res;
};
边栏推荐
- Mit.js: small event publishing and subscription library
- Introduction to kubernetes
- "Niuke | daily question" inverse Polish expression
- [wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)
- Compile ue5.0
- Teach you how to locate unreasonable SQL? And optimize it
- Gradle test and idea test
- [wechat applet] picker scroll selector (85/100)
- 2022-07-24: what is the output of the following go language code? A:[]int{}; B:[]int(nil); C:panic; D: Compilation error. package main import ( “fmt“ ) f
- Completed project series Tutorials - smart campus management system
猜你喜欢
![2022-07-24: what is the output of the following go language code? A:[]int{}; B:[]int(nil); C:panic; D: Compilation error. package main import ( “fmt“ ) f](/img/bf/e38a8fd813f88a83f61a1abfa3b95d.png)
2022-07-24: what is the output of the following go language code? A:[]int{}; B:[]int(nil); C:panic; D: Compilation error. package main import ( “fmt“ ) f

epoll的实现原理

Implementation principle of epoll

Detailed explanation of security authentication of mongodb

OA and fansoft Bi cross system users, departments and posts synchronous summary

HMS core discovery Episode 16 live broadcast preview | play AI's new "sound" state with tiger pier

Openworm project compilation

一篇文章带你读懂Redis的哨兵模式

ESWC 2018 | R-GCN:基于图卷积网络的关系数据建模

When we talk about immutable infrastructure, what are we talking about
随机推荐
How to merge cells in a table by markdown
Basic knowledge of scratch crawler framework
Novel capture practice
OA and fansoft Bi cross system users, departments and posts synchronous summary
深入掌握Pod
Three must know and know problems of redis
Why does the official not recommend using UUID as MySQL primary key
Which website is suitable for physical server
Androd releases jitpack open source project (gradle7.2)
Redis的三个必知必会的问题
学习记录[email protected]研发效能度量指标
Logu p3398 hamsters find sugar solution
Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
Pikachu vulnerability platform exercise
Gbase 8A about no suitable driver
rhcsa暑假第二天
85 distributed project construction
2022-7-18 summary
很多时候都是概率
Compile ue5.0