当前位置:网站首页>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;
};
边栏推荐
- 使用getifaddrs获取本机网口IP地址
- Document collaboration tool recommendation
- An article takes you to understand the sentinel mode of redis
- Pyg builds GCN to realize link prediction
- Summary of UPR optimization suggestions of unity
- How can I check if the number of RDS links in MySQL suddenly rises?
- DOM在Ahooks中的处理过程
- STM32 development note 117: generate IIR low-pass filter coefficients using MATLAB
- Matter's Unified Smart Home connection standard enables local automatic interaction between smart devices
- 小说抓取实战
猜你喜欢

Small case of data analysis: visualize recruitment data and view the most needed technologies in the field~
Set up private CA server
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

Paper:《Peeking Inside the Black Box: Visualizing Statistical Learning with Plots of Individual Condi

QT download installation tutorial

Actual combat | record an attack and defense drill management

Implementation principle of epoll

【基于stm32f103的SHT30温湿度显示】

MCU experiment record

Teach you how to locate unreasonable SQL? And optimize it
随机推荐
[analysis of GPIO register (crl/crh) configuration of STM32]
STM32 development note 117: generate IIR low-pass filter coefficients using MATLAB
Token value replacement of burpsuite blasting
In depth understanding of service
[wechat applet] design and interactive implementation of auction product details page (including countdown and real-time update of bids)
How to publish your own NPM package
Detailed explanation of security authentication of mongodb
It we media shows off its wealth in a high profile, and is targeted by hacker organizations. It is bound to be imprisoned
rhcsa暑假第二天
ES6 -- Methods and extensions of array objects, traversal of arrays, and extension methods of strings
I will write some Q & A blogs recently, mainly focusing on the points that are easy to have doubts.
HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
龙蜥社区发布首个 Anolis OS 安全指南 为用户业务系统保驾护航
etcd学习
QT download installation tutorial
STM32 Development Notes 119: what macros are required to enable FPU?
Druid connection pool - strong self-study from 0. Those who don't understand Druid can click in. If you know not to click in, you will think I'm wordy
深入掌握Service
Tiny-emitter.js: a small event subscription and Publishing Library
rhcsa暑假第三天