当前位置:网站首页>LeetCode最长序列
LeetCode最长序列
2022-06-09 12:56:00 【zdb呀】
最长序列
300. 最长递增子序列【中等】

- 方法一:动态规划
时间复杂度:O(n^2)
空间复杂度:O(n)
1、确定dp数组及其含义
dp[i]表示0~i索引的最长上升子序列长度
2、确定状态转移方程
if(nums[i] > nums[j]) dp[i] = max(dp[i], dp[j]+1);
3、dp初始化
dp[i] = 1;,只要nums有长度,至少为1
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
if(nums.size() <= 1) return nums.size();
// dp初始化
vector<int> dp(nums.size(), 1);
int maxLen = 0;
for (int i = 1; i < nums.size(); ++i) {
//以i结尾的nums的最长上升子序列
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;
}
};
方法二:贪心+二分查找
时间复杂度:O(nlogn)
空间复杂度:O(n)
674. 最长连续递增序列【简单】

思路一:动态规划
时间复杂度:O(n)
空间复杂度:O(n)
1、确定dp数组及其下标含义
dp[i]是索引0~i时连续递增子序列长度
2、确定递推公式
if(nums[i] > nums[i-1]) dp[i] = dp[i-1] + 1;
3、dp初始化
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]) // 连续记录
dp[i] = dp[i-1] + 1;
maxLen = max(maxLen, dp[i]);
}
return maxLen;
}
};
- 思路二:贪心
时间复杂度:O(n)
空间复杂度: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;
}
};
思路三:双指针
时间复杂度:O(n)
空间复杂度: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;
}
}
//最后一节
if(nums.size() - start > maxLen) maxLen = nums.size() - start;
return maxLen;
}
};
718. 最长重复子数组【中等】

思路:动态规划
时间复杂度:O(mn)
空间复杂度:O(mn)
1、确定dp数组及其下标含义
dp[i][j]:以下标i-1结尾的A,j-1结尾的B,它们的最长重复子数组长度
2、确定递推公式
dp[i][j] = dp[i-1][j-1] + 1;
3、dp初始化
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;
}
};
思路二:空间优化,滚动数组
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; // 注意这里不相等的时候要有赋0的操作
maxLen = max(maxLen, dp[j]);
}
}
return maxLen;
}
};
1143. 最长公共子序列【中等】


- 思路:动态规划
时间复杂度:O(mn)
空间复杂度:O(mn)
1、确定dp数组以及下标含义
dp[i][j]:索引为0-i的text1与0-j的text2的最长公共子序列
2、确定递推公式
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初始化
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()];
}
};
边栏推荐
- 临界区、事件、互斥量、 信号量--四种控制多线程同步与互斥的方法
- Mysql database (25): foreing key
- 【深度优先搜索】最大乘积:排列
- 6000 字+,帮你搞懂互联网架构演变历程!
- 驻美国大使馆提醒在美中国公民注意暑期出行安全
- 记录下bilibili(b站)小火箭页面上划动画效果的实现
- UniswapV2周边合约学习(五)-- ExampleFlashSwap.sol
- 占位智能家居市场,施耐德电气仅靠一个Wiser系统?
- 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
- Yunna | what does database monitoring generally monitor
猜你喜欢
Prototype chain? New misconceptions

斯坦福博士提出超快省显存Attention,GPT-2训练速度提升3.5倍,BERT速度创纪录

Take six five stars in Tencent (Part 2)

Yunna | what does database monitoring generally monitor

Seven misconceptions of digital transformation

Hit the snake seven inches

Analysis of network visualization analysis technology

Explain asynchronous tasks in detail: the task of function calculation triggers de duplication

Yunna intelligent operation and maintenance management system platform, visual operation and maintenance system management

常见的图像分割方法
随机推荐
面试题 05.08. 绘制直线
win11启用多用户远程桌面同时登陆
2022.6.5-----leetcode.478
网络摄像头RTSP视频流WEB端实时播放实现方案
2022.6.7-----leetcode.875
Analysis of network visualization analysis technology
Explain asynchronous tasks in detail: the task of function calculation triggers de duplication
Uniswapv2 peripheral contract learning (VIII) -- exampleswaptoprice sol
Uniswap contract learning -- uniswap uni token
打蛇打七寸
对某快捷酒店一次内网测试
Common image segmentation methods
记录下bilibili(b站)小火箭页面上划动画效果的实现
使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
Introduction to erc721 non homogenous token (NFT)
【并查集】合根植物(连通块的数数量)
15 Uncaught TypeError: Cannot set properties of null (setting ‘onclick‘)
Yunna database monitoring tool, database monitoring operation and maintenance tool
UniswapV2周边合约学习(八)-- ExampleSwapToPrice.sol
【深度优先搜索】玩具蛇:迷宫问题