当前位置:网站首页>Dynamic planning for solving problems (6)
Dynamic planning for solving problems (6)
2022-07-27 06:09:00 【RL-UAV】
300. The longest increasing subsequence
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
if (nums.size() <= 1) return nums.size();
vector<int> dp(nums.size(), 1);
int result = 0;
for (int i = 1; i < nums.size(); i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) dp[i] = max(dp[i], dp[j] + 1);
}
if (dp[i] > result) result = dp[i]; // Long subsequence
}
return result;
}
};
674. The longest continuous increasing sequence
summary : Before the discontinuous increasing subsequence 0-i The states are related to , Continuously increasing subsequences are only related to the previous state
Be careful :
i < nums.size() - 1;
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
if (nums.size() == 0) return 0;
int result = 1;
vector<int> dp(nums.size() ,1);
for (int i = 0; i < nums.size() - 1; i++) {
if (nums[i + 1] > nums[i]) {
// Continuous record
dp[i + 1] = dp[i] + 1;
}
if (dp[i + 1] > result) result = dp[i + 1];
}
return result;
}
};
718. Longest repeating subarray
summary :
dp[i][j] = dp[i - 1][j - 1] + 1;
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 result = 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;
}
if (dp[i][j] > result) result = dp[i][j];
}
}
return result;
}
};
Scrolling array Two dimensional to one dimensional array
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
vector<int> dp(vector<int>(B.size() + 1, 0));
int result = 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
if (dp[j] > result) result = dp[j];
}
}
return result;
}
};
1143. Longest common subsequence
summary :
dpi: The length is [0, i - 1] String text1 And length [0, j - 1] String text2 The longest common subsequence of is dpi
dpi = dpi - 1 + 1; dpi = max(dpi - 1, dpi);
error :i <= text1.size(); j <= text2.size()
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()];
}
};
1035. Disjoint lines
summary :
ah It's exactly the same as the last one , I don't know another template .
class Solution {
public:
int maxUncrossedLines(vector<int>& A, vector<int>& B) {
vector<vector<int>> dp(A.size() + 1, vector<int>(B.size() + 1, 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;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[A.size()][B.size()];
}
};
53. The largest subsequence and
summary :
error :dp[0] = nums[0];
int result = dp[0]; Initialize to 0
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if (nums.size() == 0) return 0;
vector<int> dp(nums.size());
dp[0] = nums[0];
int result = dp[0];
for (int i = 1; i < nums.size(); i++) {
dp[i] = max(dp[i - 1] + nums[i], nums[i]); // State transition formula
if (dp[i] > result) result = dp[i]; // result preservation dp[i] The maximum of
}
return result;
}
};
- Time complexity :O(n)
- Spatial complexity :O(n)
392. Judging subsequences
dpi Denotes the following i-1 String ending with s, And the following j-1 String ending with t, The length of the same subsequence is dpi.
class Solution {
public:
bool isSubsequence(string s, string t) {
vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1, 0));
for (int i = 1; i <= s.size(); i++) {
for (int j = 1; j <= t.size(); j++) {
if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
else dp[i][j] = dp[i][j - 1];
}
}
if (dp[s.size()][t.size()] == s.size()) return true;
return false;
}
};
- Time complexity :O(n × m)
- Spatial complexity :O(n × m)
边栏推荐
猜你喜欢

Leetcode每日一题30. 串联所有单词的子串

超强远程连接管理工具:Royal TSX

arcgis for js api(2) 获取要素服务的id集合

哈希表的原理及哈希冲突的解决方法

Li Kou 236. the nearest common ancestor of binary tree

Live Home 3D Pro interior home design tool

力扣每日一题leetcode 513. 找树左下角的值
![[first song] machine learning of rebirth - linear regression](/img/70/3efd9eacf88f55022eb52d096926f7.png)
[first song] machine learning of rebirth - linear regression
acwing每日一题 正方形数组的数目

Cesium教程 (1) 界面介绍-3dtiles加载-更改鼠标操作设置
随机推荐
力扣题解 动态规划(3)
Force buckle 160. intersecting linked list
[first song] rebirth of me in py introductory training (4): Circular program
论文写作(收获)
Leetcode每日一题30. 串联所有单词的子串
力扣题解 动态规划(7)
力扣题解 二叉树(5)
[song] rebirth of me in py introductory training (7): function call
力扣题解 动态规划(6)
LaTeX中多个公式公用一个序号时
力扣题解 单调栈
浅记一下十大排序
力扣 236. 二叉树的最近公共祖先
这是我的博客
Linked list palindrome judgment
Lightroom classic 2022 v11.4 Chinese version "latest resources"
[song] rebirth of me in py introductory training (9): exception handling
C语言-动态内存管理
Lightroom Classic 2022 v11.4中文版「最新资源」
arcgis style样式表文件转换成geoserver sld文件