当前位置:网站首页>力扣题解 动态规划(3)
力扣题解 动态规划(3)
2022-07-27 05:20:00 【RL-UAV】
0.1完全背包
/ 先遍历物品,在遍历背包
void test_CompletePack() {
vector<int> weight = {
1, 3, 4};
vector<int> value = {
15, 20, 30};
int bagWeight = 4;
vector<int> dp(bagWeight + 1, 0);
for(int i = 0; i < weight.size(); i++) {
// 遍历物品
for(int j = weight[i]; j <= bagWeight; j++) {
// 遍历背包容量
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
}
cout << dp[bagWeight] << endl;
}
int main() {
test_CompletePack();
}
518. 零钱兑换 II
是一道典型的背包问题,一看到钱币数量不限,就知道这是一个完全背包
在求装满背包有几种方案的时候,认清遍历顺序是非常关键的。
如果求组合数就是外层for循环遍历物品,内层for遍历背包。
如果求排列数就是外层for遍历背包,内层for循环遍历物品。
dp[j]:凑成总金额j的货币组合数为dp[j]
所以递推公式:dp[j] += dp[j - coins[i]];
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<int> dp(amount + 1, 0);
dp[0] = 1;
for (int i = 0; i < coins.size(); i++) {
// 遍历物品
for (int j = coins[i]; j <= amount; j++) {
// 遍历背包
dp[j] += dp[j - coins[i]];
}
}
return dp[amount];
}
};
377. 组合总和 Ⅳ
C++测试用例有两个数相加超过int的数据,所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> dp(target + 1, 0);
dp[0] = 1;
for (int i = 0; i <= target; i++) {
// 遍历背包
for (int j = 0; j < nums.size(); j++) {
// 遍历物品
if (i - nums[j] >= 0 && dp[i] < INT_MAX - dp[i - nums[j]]) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
};
70. 爬楼梯
class Solution {
public:
int climbStairs(int n) {
vector<int> dp(n + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; i++) {
// 遍历背包
for (int j = 1; j <= m; j++) {
// 遍历物品
if (i - j >= 0) dp[i] += dp[i - j];
}
}
return dp[n];
}
};
代码中m表示最多可以爬m个台阶,代码中把m改成2就是本题70.爬楼梯可以AC的代码了。
322. 零钱兑换
而本题是要求最少硬币数量,硬币是组合数还是排列数都无所谓!所以两个for循环先后顺序怎样都可以!
// 版本一
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, INT_MAX);
dp[0] = 0;
for (int i = 0; i < coins.size(); i++) {
// 遍历物品
for (int j = coins[i]; j <= amount; j++) {
// 遍历背包
if (dp[j - coins[i]] != INT_MAX) {
// 如果dp[j - coins[i]]是初始值则跳过
dp[j] = min(dp[j - coins[i]] + 1, dp[j]);
}
}
}
if (dp[amount] == INT_MAX) return -1;
return dp[amount];
}
};
对于遍历方式遍历背包放在外循环,遍历物品放在内循环也是可以的,我就直接给出代码了
// 版本一
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, INT_MAX);
dp[0] = 0;
for (int i = 0; i < coins.size(); i++) {
// 遍历物品
for (int j = coins[i]; j <= amount; j++) {
// 遍历背包
if (dp[j - coins[i]] != INT_MAX) {
// 如果dp[j - coins[i]]是初始值则跳过
dp[j] = min(dp[j - coins[i]] + 1, dp[j]);
}
}
}
if (dp[amount] == INT_MAX) return -1;
return dp[amount];
}
};
边栏推荐
- 数据库索引的一些说明以及使用
- ps 2022 六月更新,都新增了哪些功能
- C语言--字符串操作函数与内存操作函数
- 【头歌】重生之我在py入门实训中(4):循环程序
- [first song] rebirth of me in py introductory training (6): definition and application of functions
- IOT operating system
- [song] rebirth of me in py introductory training (12): Matplotlib interface and common graphics
- 解决conda install 安装停止、中断问题
- 为什么交叉熵损失可以用于刻画损失
- 【5·20特辑】MatLAb之我在和你表白
猜你喜欢
随机推荐
【第一篇博客-展望】
视觉横向课题bug1:FileNotFoundError: Could not find module ‘MvCameraControl.dll‘ (or one of it
Cesium教程 (1) 界面介绍-3dtiles加载-更改鼠标操作设置
A photo breaks through the face recognition system: you can nod your head and open your mouth, netizens
arcgis for js api-入门系列
制作视频后期特效需要什么工具?
3. Classification problems - initial experience of handwritten digit recognition
西瓜书第三章---线性模型学习笔记
Greedy high performance neural network and AI chip application research and training
16. Over fitting and under fitting
C语言扫雷最新 递归展开 超详解(附源码)
[first song] rebirth of me in py introductory training (4): Circular program
pytorch转onnx相关问题
[song] rebirth of me in py introductory training (7): function call
arcgis for js api(1) 获取featureLayer的所有字段名
C语言-程序的编译
[high concurrency] interviewer
Live Home 3D Pro interior home design tool
Dpdk network protocol stack VPP OVS DDoS Sdn nfv virtualization high performance expert Road
Lightroom Classic 2022 v11.4中文版「最新资源」



![[MySQL learning] 8](/img/25/84d5acbdd8aba3455ab8e3eb17dfa8.png)





