当前位置:网站首页>【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;
}
}
边栏推荐
- Live app source code, jump to links outside the station or jump to pages inside the platform
- Iclr2022: how does AI recognize "things I haven't seen"?
- 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
- X Opencv feature point detection and matching
- 2022 system integration project management engineer examination knowledge points: software development model
- P1629 postman delivering letter
- Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
- Interesting 10 CMD commands
- Introduction to the gtid mode of MySQL master-slave replication
- Les sociétés de valeurs mobilières dont la Commission d'ouverture d'un compte d'actions est la plus faible ont ce que tout le monde recommande.
猜你喜欢

2022.02.14

Design of logic level conversion in high speed circuit

2022 t elevator repair registration examination and the latest analysis of T elevator repair

EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance

A treasure open source software, cross platform terminal artifact tabby

Gorilla/mux framework (RK boot): add tracing Middleware

How to make icons easily

Double efficiency. Six easy-to-use pychar plug-ins are recommended

It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something

Enter MySQL in docker container by command under Linux
随机推荐
A treasure open source software, cross platform terminal artifact tabby
Gossip about redis source code 76
How will the complete NFT platform work in 2022? How about its core functions and online time?
Introduction to the gtid mode of MySQL master-slave replication
在恒泰证券开户怎么样?安全吗?
2022 t elevator repair registration examination and the latest analysis of T elevator repair
How to prevent malicious crawling of information by one-to-one live broadcast source server
What are the securities companies with the lowest Commission for stock account opening? Would you recommend it? Is it safe to open an account on your mobile phone
ADB related commands
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
IO flow principle and classification
D24:divisor and multiple (divisor and multiple, translation + solution)
Smart fan system based on stm32f407
[Mongodb] 2. Use mongodb --------- use compass
The first game of the new year, many bug awards submitted
Subgraph isomorphism -subgraph isomorphism
Distributed transaction -- middleware of TCC -- selection / comparison
Les sociétés de valeurs mobilières dont la Commission d'ouverture d'un compte d'actions est la plus faible ont ce que tout le monde recommande.
Unity shader visualizer shader graph
Enter MySQL in docker container by command under Linux