当前位置:网站首页>【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;
}
}
边栏推荐
- Make small tip
- [MySQL] sql99 syntax to realize multi table query
- Selenium check box
- [note] glide process and source code analysis
- Selenium library 4.5.0 keyword explanation (4)
- Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
- D27:mode of sequence (maximum, translation)
- D28:maximum sum (maximum sum, translation)
- Open 2022 efficient office, starting from project management
- Iclr2022: how does AI recognize "things I haven't seen"?
猜你喜欢

Actual combat | use composite material 3 in application

Pytorch learning notes 5: model creation

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
![[Happy Valentine's day]](/img/d9/9280398eb64907a567df6eea772adb.jpg)
[Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)

Pyqt5 sensitive word detection tool production, operator's Gospel

A treasure open source software, cross platform terminal artifact tabby

Smart fan system based on stm32f407

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

How to understand the gain bandwidth product operational amplifier gain

Enter MySQL in docker container by command under Linux
随机推荐
Report on prospects and future investment recommendations of China's assisted reproductive industry, 2022-2028 Edition
Gossip about redis source code 81
P1629 postman delivering letter
Kubedl hostnetwork: accelerating the efficiency of distributed training communication
"Learning notes" recursive & recursive
The difference between single power amplifier and dual power amplifier
"Learning notes" recursive & recursive
NLP Chinese corpus project: large scale Chinese natural language processing corpus
Pandaoxi's video
Schematic diagram of crystal oscillator clock and PCB Design Guide
Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
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.
Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
Fashion cloud interview questions series - JS high-frequency handwritten code questions
Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition
Alibaba cloud container service differentiation SLO hybrid technology practice
D29:post Office (post office, translation)
D24:divisor and multiple (divisor and multiple, translation + solution)
How to quickly build high availability of service discovery
Tencent interview: can you find the number of 1 in binary?