当前位置:网站首页>Leetcode longest sequence
Leetcode longest sequence
2022-06-09 14:07:00 【ZDB】
The longest sequence
300. The longest increasing subsequence 【 secondary 】

- Method 1 : Dynamic programming
Time complexity :O(n^2)
Spatial complexity :O(n)
1、 determine dp Array and its meaning
dp[i]Express 0~i Length of the longest ascending subsequence of the index
2、 Determine the state transfer equation
if(nums[i] > nums[j]) dp[i] = max(dp[i], dp[j]+1);
3、dp initialization
dp[i] = 1;, as long as nums There's a length , At least for 1
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
if(nums.size() <= 1) return nums.size();
// dp initialization
vector<int> dp(nums.size(), 1);
int maxLen = 0;
for (int i = 1; i < nums.size(); ++i) {
// With i At the end of the nums The longest ascending subsequence of
for (int j = 0; j < i; ++j) {
if (nums[j] < nums[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
maxLen = max(maxLen, dp[i]);
}
return maxLen;
}
};
Method 2 : greedy + Two points search
Time complexity :O(nlogn)
Spatial complexity :O(n)
674. The longest continuous increasing sequence 【 Simple 】

Train of thought : Dynamic programming
Time complexity :O(n)
Spatial complexity :O(n)
1、 determine dp Array and its subscript meaning
dp[i]It's the index 0~i Time continuous increment subsequence length
2、 Determine the recurrence formula
if(nums[i] > nums[i-1]) dp[i] = dp[i-1] + 1;
3、dp initialization
vector<int> dp(nums.size(), 1);
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
if (nums.size() <= 1) return nums.size();
int maxLen = 1;
vector<int> dp(nums.size() ,1);
for (int i = 1; i < nums.size(); i++) {
if (nums[i] > nums[i-1]) // Continuous record
dp[i] = dp[i-1] + 1;
maxLen = max(maxLen, dp[i]);
}
return maxLen;
}
};
- Train of thought two : greedy
Time complexity :O(n)
Spatial complexity :O(1)
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
if (nums.size() <= 1) return nums.size();
int maxLen = 1;
int count = 1;
for (int i = 1; i < nums.size(); i++) {
if (nums[i] > nums[i-1]) count++;
else count = 1;
maxLen = max(maxLen, count);
}
return maxLen;
}
};
Train of thought three : Double pointer
Time complexity :O(n)
Spatial complexity :O(1)
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
if (nums.size() <= 1) return nums.size();
int maxLen = 1;
int start = 0;
for(int end = 1; end < nums.size(); ++end){
if(nums[end] <= nums[end-1]){
maxLen = max(maxLen, end - start);
start = end;
}
}
// The last section
if(nums.size() - start > maxLen) maxLen = nums.size() - start;
return maxLen;
}
};
718. Longest repeating subarray 【 secondary 】

Ideas : Dynamic programming
Time complexity :O(mn)
Spatial complexity :O(mn)
1、 determine dp Array and its subscript meaning
dp[i][j]: By subscript i-1 At the end of the A,j-1 At the end of the B, Their longest repeating subarray length
2、 Determine the recurrence formula
dp[i][j] = dp[i-1][j-1] + 1;
3、dp initialization
dp[i][0] = 0;dp[0][j] = 0;
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
vector<vector<int>> dp (A.size() + 1, vector<int>(B.size() + 1, 0));
int maxLen = 0;
for (int i = 1; i <= A.size(); i++) {
for (int j = 1; j <= B.size(); j++) {
if (A[i - 1] == B[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
maxLen = max(maxLen, dp[i][j]);
}
}
return maxLen;
}
};
Train of thought two : Space optimization , Scrolling array
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
vector<int> dp(vector<int>(B.size() + 1, 0));
int maxLen = 0;
for (int i = 1; i <= A.size(); i++) {
for (int j = B.size(); j > 0; j--) {
if (A[i - 1] == B[j - 1]) {
dp[j] = dp[j - 1] + 1;
}
else dp[j] = 0; // Note that when there is inequality here, there should be fu 0 The operation of
maxLen = max(maxLen, dp[j]);
}
}
return maxLen;
}
};
1143. Longest common subsequence 【 secondary 】


- Ideas : Dynamic programming
Time complexity :O(mn)
Spatial complexity :O(mn)
1、 determine dp Array and subscript meaning
dp[i][j]: The index for 0-i Of text1 And 0-j Of text2 The longest common subsequence of
2、 Determine the recurrence formula
if(text1[i-1] == text2[j-1]) dp[i][j] = dp[i-1][j-1] + 1;if(text1[i-1] != text2[j-1]) dp[i][j] != max(dp[i-1][j], dp[i][j-1]);
3、dp initialization
vector<vector<int>> dp(text1.size(), vector<int>(text2.size(), 0));
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
vector<vector<int>> dp(text1.size()+1, vector<int>(text2.size()+1, 0));
for(int i = 1; i <= text1.size(); i++){
for(int j = 1; j <= text2.size(); j++){
if(text1[i-1] == text2[j-1]){
dp[i][j] = dp[i-1][j-1] + 1;
}
else{
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
return dp[text1.size()][text2.size()];
}
};
边栏推荐
猜你喜欢

What's the use of finding amino acids in extraterrestrial life?

4427 树中节点和(贪心)

Raspberry PI raspberry pie classification and introduction of its similar products

Cloud native essay kubernetes workload

ThreadLocal还不会?来看看!

线程池创建方式详解
![[Ji Wang] review of Cisco final multiple choice questions](/img/80/e84d8b2a5873b2d215463a503bebf7.png)
[Ji Wang] review of Cisco final multiple choice questions

Understand Huawei's "Three Outlooks" in these root technologies

6000 字+,帮你搞懂互联网架构演变历程!

【并查集】合根植物(连通块的数数量)
随机推荐
At the age of 26, he published 18 papers and just proved the prime number conjecture of the last century
面试题 05.04. 下一个数
常见的图像分割方法
智慧农业小麦室外病虫害防治规范,北斗农业
浅谈RedisTemplate和StringRedisTemplate的区别
谁说Redis不能存大key
駐美國大使館提醒在美中國公民注意暑期出行安全
C语言 结构体 | 链表
Wechat applet
Import word document picture VM virtual machine network settings
leetcode:497. 非重叠矩形中的随机点【随缘随机 + 前缀和二分 + 过了就行】
面试题 08.01. 三步问题
Teach you how to implement a virtual machine with JS
Common image segmentation methods
ThreadLocal基础及高级使用
Dr. Stanford put forward the idea of ultra fast and saving memory attention. The gpt-2 training speed was increased by 3.5 times, and the Bert speed reached a record
51单片机定时器0作为时间基准以及延时函数参考使用
进程,时间片,并发与并行
2022.5.30-----leetcode.1022
51 MCU timer 0 is used as time reference and delay function reference