当前位置:网站首页>【leetcode】300. Longest increasing subsequence (dynamic programming, dichotomy)
【leetcode】300. Longest increasing subsequence (dynamic programming, dichotomy)
2022-07-03 23:50:00 【Chinese fir sauce_】
subject :
300. The longest increasing subsequence
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
Method 1 : Dichotomy :
class Solution {
public int lengthOfLIS(int[] nums) {
int res = 0;
int len = nums.length;
int[] dp = new int[len];
for(int n : nums){
int i = Arrays.binarySearch(dp,0,res,n);
if(i < 0){
i = -(i+1);
}
dp[i] = n;
if(i == res){
res++;
}
}
return res;
}
}
- Time complexity :O(n log n)
- Spatial complexity :O(n)
Method 2 : Dynamic programming
Ideas :
State definition :
dp[i] The value of represents nums With nums[i] The length of the longest subsequence at the end .
Transfer equation : set up j∈[0,i), Consider each round to calculate the new dp[i] when , Traverse [0,i) List intervals , Make the following judgment :
① When nums[i]>nums[j] when : nums[i] It can be connected to nums[j] after ( This problem requires strict increment ), In this case, the length of the longest ascending subsequence is dp[j] + 1;
② When nums[i] <= nums[j] when : nums[i] Can't connect to nums[j] after , In this case, the ascending subsequence does not hold , skip .
- All of the above 1. situation Calculated under dp[j] + 1 The maximum of , Until i The length of the longest ascending subsequence of ( namely dp[i]). The implementation method is traversal j when , Each round of execution dp[i] = max(dp[i], dp[j] + 1).
- Transfer equation : dp[i] = max(dp[i], dp[j] + 1) for j in [0, i).
The initial state :
dp[i]dp[i] All elements are set to 1, The meaning is that each element can at least be a separate subsequence , At this time, the length is 1.
Return value :
return dpdp List maximum , The global longest ascending subsequence length can be obtained .
Complexity analysis :
Time complexity O(N^2): Traversal calculation dp List needs O(N), Calculate each dp[i] Need to be O(N).
Spatial complexity O(N) : dp The list takes up extra space of linear size .
// Dynamic programming.
class Solution {
public int lengthOfLIS(int[] nums) {
int len = nums.length;
if(len == 0) return 0;
int res = 0;
int[]dp = new int[len];
// Array with 1 fill
Arrays.fill(dp, 1);
for(int i = 0; i < len;i++){
for(int j = 0;j < i;j++) {
if(nums[i] > nums[j]) dp[i] = Math.max(dp[i],(dp[j]+1));
}
// Record each round of dp[i]
res = Math.max(res,dp[i]);
}
return res;
}
}
边栏推荐
- Correlation analysis summary
- 2022.02.14
- 2022 system integration project management engineer examination knowledge points: software development model
- Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
- 2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Gossip about redis source code 81
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii
- Briefly understand the operation mode of developing NFT platform
猜你喜欢
Idea set class header comments
Briefly understand the operation mode of developing NFT platform
Common mode interference of EMC
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Zipper table in data warehouse (compressed storage)
2022.02.13
A treasure open source software, cross platform terminal artifact tabby
Cgb2201 preparatory class evening self-study and lecture content
leetcode-43. String multiplication
随机推荐
A treasure open source software, cross platform terminal artifact tabby
Actual combat | use composite material 3 in application
[MySQL] classification of multi table queries
Pyqt5 sensitive word detection tool production, operator's Gospel
How to write a good title of 10w+?
leetcode-43. String multiplication
2022 free examination questions for hoisting machinery command and hoisting machinery command theory examination
Introducing Software Testing
2/14 (regular expression, sed streaming editor)
The difference between single power amplifier and dual power amplifier
QT creator source code learning note 05, how does the menu bar realize plug-in?
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
A method to solve Bert long text matching
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
Selenium library 4.5.0 keyword explanation (4)
Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
2022 t elevator repair registration examination and the latest analysis of T elevator repair
Subgraph isomorphism -subgraph isomorphism