当前位置:网站首页>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;
}
边栏推荐
- Chrome process architecture
- Li Kou 279 complete square - dynamic programming
- "Introduction to interface testing" punch in to learn day04: how to abstract the procedural test script into a test framework?
- New features commonly used in ES6
- Dc-2-range practice
- Force deduction brush question 14. Longest common prefix
- Use reflection to convert RDD to dataframe
- Analysis of DNS domain name resolution process
- Handwriting promise
- What should testers do if they encounter a bug that is difficult to reproduce?
猜你喜欢

Hw2021 attack and defense drill experience - Insights

Advantages and disadvantages of zero trust security

Openlayers ol ext: Transform object, rotate, stretch, zoom in

How is the virtual DOM different from the actual DOM?

Flink1.15 source code reading - Flink annotations

mysql_ Case insensitive

Detailed explanation of three factory modes

Bubble sort / heap sort

Force the resumption of game 302 of the week

Direct insert sort / Hill sort
随机推荐
Moveit2 - 10.urdf and SRDF
Leetcode programming practice -- Tencent selected 50 questions (I)
Optimization of MySQL sorting index fields
Database transactions (often asked)
Riotboard development board series notes (6) -- buildreoot building system image
Function of each layer of data warehouse
Riotboard development board series notes (VII) -- the use of framebuffer
Select sort / cardinality sort
Direct insert sort / Hill sort
Take a note: Oracle conditional statement
What is technical support| Daily anecdotes
Enter an integer and a binary tree
Analysis of DNS domain name resolution process
Riotboard development board series notes (V) -- porting u-boot
NVM installation and use
Test question C: question brushing statistics
Introduction to Apache Doris grafana monitoring indicators
Machine learning notes - building a recommendation system (4) matrix decomposition for collaborative filtering
ECMAScript new features
C. Mark and His Unfinished Essay