当前位置:网站首页>300. Longest increasing subsequence
300. Longest increasing subsequence
2022-07-25 03:27:00 【Xiao Lu wants to brush the questions】
List of articles
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 .
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 .
One 、O(n2) Solution
Dynamic programming , Model from left to right

dp[i] Subsequence must be i At the end of the number of positions , How long is the longest increasing subsequence ? Whole middle demand max That's the answer.
Every time dp[i] when , Traversal array 0 To i-1, Find out the ratio nums[i] The position of a small number j,
dp[i]=Math.max(dp[i],dp[j]+1);
Code
class Solution {
public int lengthOfLIS(int[] nums) {
int n=nums.length;
if(n==0){
return 0;
}
int max=1;
int[] dp=new int[n];
dp[0]=1;
for(int i=1;i<n;i++){
dp[i]=1;
for(int j=0;j<i;j++){
if(nums[i]>nums[j]){
dp[i]=Math.max(dp[i],dp[j]+1);
}
}
max=Math.max(max,dp[i]);
}
return max;
}
}
Optimal solution

auxiliary end Array
end[i]: At present, all lengths are i+ 1 The smallest ending in the increasing subsequence of
How to fill in end?
One number at a time num[i], With two points in end The leftmost greater than... Is found in the array num[i] The location of j
end[j]=num[i];
If not greater than num[i] Number of numbers , be end[j+1]=num[i]

After you put in a value , The position meaning of rewriting is right , The position that has not been rewritten continues to ensure the correct meaning
public static int lengthOfLIS(int[] arr) {
if (arr == null || arr.length == 0) {
return 0;
}
int[] ends = new int[arr.length];
ends[0] = arr[0];
int right = 0;
int l = 0;
int r = 0;
int m = 0;
int max = 1;
for (int i = 1; i < arr.length; i++) {
l = 0;
r = right;
while (l <= r) {
m = (l + r) / 2;
if (arr[i] > ends[m]) {
l = m + 1;
} else {
r = m - 1;
}
}
right = Math.max(right, l);
ends[l] = arr[i];
max = Math.max(max, l + 1);
}
return max;
}
边栏推荐
- Moveit2 - 7. Scenario planning ROS API
- New features commonly used in ES6
- Can bus baud rate setting of stm32cubemx
- How does Jupiter notebook change themes and font sizes?
- Direct insert sort / Hill sort
- How to use two stacks to simulate the implementation of a queue
- Advantages and disadvantages of zero trust security
- Secondary vocational network security skills competition P100 vulnerability detection
- A queue of two stacks
- C language introduction practice (9): completion judgment
猜你喜欢

Dc-2-range practice

Machine learning exercise 8 - anomaly detection and recommendation system (collaborative filtering)
![[Kali's sshd service is enabled]](/img/1b/180534d51049177254e30c4b783eba.png)
[Kali's sshd service is enabled]

kettle_ Configure database connection_ report errors

Dc-1-practice

NC | progress has been made in the study of the ecological network relationship between dissolved organic carbon and microorganisms in the context of global change

How to use two queues to simulate the implementation of a stack

Import and export using poi

B. Making Towers

04 -- two ways of writing el and data
随机推荐
Using one stack to sort another stack
B. Difference of GCDs
Flowlayout in compose
Advantages and disadvantages of zero trust security
Function method encapsulation -- mutual conversion of image types qpixmap, qimage and mat
Uni app configuration
JS method encapsulation summary
Chrome debugging skills
Force deduction brush question 26. Delete duplicates in the ordered array
From input URL to page presentation
Day 9 (capture traffic and routing strategy)
Use and introduction of vim file editor
C language_ Defining structures and using variables
Unity: text input box for numerical judgment
What should testers do if they encounter a bug that is difficult to reproduce?
Network security - comprehensive penetration test -cve-2018-10933-libssh maintain access
Modulenotfounderror: no module named 'pyemd' solution
Leetcode.745. prefix and suffix search____ Double dictionary tree + double pointer
CVPR 2020 | social stgcnn: pedestrian trajectory prediction based on graph convolution
Recursive and non recursive methods are used to realize the first order, middle order and second order traversal of binary tree respectively