文章目录
- [63. 不同路径 II](https://leetcode-cn.com/problems/unique-paths-ii/)
- [343. 整数拆分](https://leetcode-cn.com/problems/integer-break/)
- [139. 单词拆分](https://leetcode-cn.com/problems/word-break/)
- [213. 打家劫舍 II](https://leetcode-cn.com/problems/house-robber-ii/)
- [337. 打家劫舍 III](https://leetcode-cn.com/problems/house-robber-iii/)
- [122. 买卖股票的最佳时机 II](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/)
- [123. 买卖股票的最佳时机 III](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/)
- [300. 最长递增子序列](https://leetcode-cn.com/problems/longest-increasing-subsequence/)
63. 不同路径 II
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector<vector<int>> map(m, vector<int>(n, 0));
for (int i = 0; i < m && obstacleGrid[i][0] == 0; ++i)
map[i][0] = 1;
for (int j = 0; j < n && obstacleGrid[0][j] == 0; ++j)
map[0][j] = 1;
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
if (obstacleGrid[i][j] == 1) continue;
map[i][j] = map[i - 1][j] + map[i][j - 1];
}
}
return map[m - 1][n - 1];
}
};
小结
- 遇到障碍dp[i][j]保持0就可以了
343. 整数拆分
class Solution {
public:
int integerBreak(int n) {
// f(n) = max{f(n), max{f(n-j)*j, (n-j)*j}}
vector<int> f(n+1);
f[2] = 1;
for (int i = 3; i < n + 1; ++i) {
for (int j = 1; j < i; ++j) {
f[i] = max(f[i], max(f[i - j] * j, (i - j) * j));
}
}
return f[n];
}
};
小结
- 递推公式:f(n) = max{f(n), max{f(n-j)*j, (n-j)*j}}
139. 单词拆分
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
// 将单词字典转换成无序的集合
unordered_set<string> wordset(wordDict.begin(), wordDict.end());
// 初始化背包
vector<bool> dp(s.size()+1, false);
dp[0] = true;
// 遍历顺序背包在外,物品在内
for (int i = 1; i <= s.size(); ++i){
for (int j = 0; j < i; ++j) {
string word = s.substr(j, i - j);
if (wordset.find(word) != wordset.end() && dp[j]) {
dp[i] = true;
}
}
}
return dp[s.size()];
}
};
小结
-分割子串的特殊性,遍历背包放在外循环,将遍历物品放在内循环更方便一些
213. 打家劫舍 II
class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() == 0) return 0;
if (nums.size() == 1) return nums[0];
int result1 = robRange(nums, 0, nums.size() - 2);
int result2 = robRange(nums, 1, nums.size() - 1);
return max(result1, result2);
}
int robRange(vector<int>& nums, int start, int end) {
if (end == start) return nums[start];
vector<int> dp(nums.size());
dp[start] = nums[start];
dp[start + 1] = max(nums[start], nums[start + 1]);
for (int i = start + 2; i <= end; ++i) {
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);
}
return dp[end];
}
};
337. 打家劫舍 III
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int rob(TreeNode* root) {
vector<int> result = robTree(root);
// 0:不偷, 1:偷
return max(result[0], result[1]);
}
vector<int> robTree(TreeNode* root) {
if (root == NULL) return {0, 0};
// 后序遍历
vector<int> left = robTree(root->left);
vector<int> right = robTree(root->right);
// 如果不偷cur
int result0 = max(left[0], left[1]) + max(right[0], right[1]);
// 如果偷cur
int result1 = root->val + left[0] + right[0];
return {result0, result1};
}
};
小结
- 后序遍历
122. 买卖股票的最佳时机 II
class Solution {
public:
int maxProfit(vector<int>& prices) {
// dp[i][0] 持有
// dp[i][1] 不持有
// dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
// dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
// 滚动数组
vector<vector<int>> dp(2, vector<int>(2));
dp[0][0] = -prices[0];
dp[0][1] = 0;
for (int i = 1; i < prices.size(); ++i) {
dp[i % 2][0] = max(dp[(i - 1) % 2][0],dp[(i - 1) % 2][1] - prices[i]);
dp[i % 2][1] = max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][0] + prices[i]);
}
return max(dp[1][1], dp[0][1]);
}
};
123. 买卖股票的最佳时机 III
class Solution {
public:
int maxProfit(vector<int>& prices) {
// 0: 第一天买入/持有
// 1: 第一天卖出/不持有
// 2: 第二天买入/持有
// 3: 第二天卖出/不持有
// dp[i][0] = max(dp[i - 1][0], -prices[i])
// dp[i][1] = max(dp[i - 1][1], dp[i - 1][0]+prices[i])
// dp[i][2] = max(dp[i - 1][2], dp[i - 1][1]-prices[i])
// dp[i][3] = max(dp[i - 1][3], dp[i - 1][2]+prices[i])
if (prices.size() == 0) return 0;
vector<vector<int>> dp(prices.size(), vector<int>(4));
dp[0][0] = -prices[0];
dp[0][2] = -prices[0];
for (int i = 1; i < prices.size(); ++i) {
dp[i][0] = max(dp[i - 1][0], -prices[i]);
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] - prices[i]);
dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] + prices[i]);
}
return dp[prices.size() - 1][3];
}
};
300. 最长递增子序列
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
// 没考虑 nums.size() 为0 、1 的情况。。。。
if (nums.size() <= 1) return nums.size();
// 问题,忘了要和前面所有的比了。。。。
// dp[i] 最长递增子序列
// dp[i] = max(dp[i], dp[j] + 1)
// 初始化为1
// 从前往后
vector<int> dp(nums.size(), 1);
int res = 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] > res) {
res = dp[i];
}
}
return res;
}
};