当前位置:网站首页>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;
};
边栏推荐
- Most of the time, it's probability
- Which website is suitable for physical server
- 956. Highest billboard pressure DP
- 教你如何定位不合理的SQL?并优化之
- Use getifaddrs to obtain the IP address of the local network interface
- HMS core discovery Episode 16 live broadcast preview | play AI's new "sound" state with tiger pier
- This low code reporting tool is needed for data analysis
- Gbase JDBC connection database exception
- 2、 Mysql database foundation
- unity 3D物体添加 点击事件
猜你喜欢

rhce第一天

Summary of UPR optimization suggestions of unity

If you don't know these 20 classic redis interview questions, don't go to the interview!

Wechat official account all article download links to get

It we media shows off its wealth in a high profile, and is targeted by hacker organizations. It is bound to be imprisoned

ESWC 2018 | r-gcn: relational data modeling based on graph convolution network

Interviewer: explain the core principle of ThreadLocal

When image component in wechat applet is used as background picture

2、 Mysql database foundation

Forwarding and sharing function of wechat applet
随机推荐
This low code reporting tool is needed for data analysis
unity 3D物体添加 点击事件
[untitled]
STM32 development note 117: generate IIR low-pass filter coefficients using MATLAB
Ora-01460: conversion request cannot be implemented or unreasonable
Mit.js: small event publishing and subscription library
[CTF learning] steganography set in CTF -- picture steganography
Etcd learning
Actual combat | record an attack and defense drill management
Introduction to CpG control network
Introduction to fundamentals of operations research [1]
China trifluoroethanol industry research and investment forecast report (2022 Edition)
How can I check if the number of RDS links in MySQL suddenly rises?
Purpose of setting novice task period in the integral system
哪种网站适合物理服务器
学习记录[email protected]研发效能度量指标
深入掌握Service
Token value replacement of burpsuite blasting
2、 Mysql database foundation
Pychart configuration pyqt5