当前位置:网站首页>Dynamic planning for solving problems (3)
Dynamic planning for solving problems (3)
2022-07-27 06:09:00 【RL-UAV】
0.1 Completely backpack
/ Go through the items first , Walking through the backpack
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++) {
// Traverse the items
for(int j = weight[i]; j <= bagWeight; j++) {
// Traverse the backpack capacity
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
}
cout << dp[bagWeight] << endl;
}
int main() {
test_CompletePack();
}
518. Change for II
It's a typical knapsack problem , There is no limit to the number of coins , I knew it was a complete backpack
When there are several ways to fill a backpack , It's critical to recognize the traversal order .
If you find the combination number, it's the outer layer for Loop through items , Inner layer for Traverse the backpack .
If you find the permutation number, it's the outer layer for Traverse the backpack , Inner layer for Loop through items .
dp[j]: Make up the total amount j The number of currency combinations is dp[j]
So the recurrence formula :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++) {
// Traverse the items
for (int j = coins[i]; j <= amount; j++) {
// Traverse the backpack
dp[j] += dp[j - coins[i]];
}
}
return dp[amount];
}
};
377. Combinatorial summation Ⅳ
C++ There are two numbers in the test case that add up to more than int The data of , So you need to be in if Riga 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++) {
// Traverse the backpack
for (int j = 0; j < nums.size(); j++) {
// Traverse the items
if (i - nums[j] >= 0 && dp[i] < INT_MAX - dp[i - nums[j]]) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
};
70. climb stairs
class Solution {
public:
int climbStairs(int n) {
vector<int> dp(n + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; i++) {
// Traverse the backpack
for (int j = 1; j <= m; j++) {
// Traverse the items
if (i - j >= 0) dp[i] += dp[i - j];
}
}
return dp[n];
}
};
In the code m It means you can climb at most m A stair , In the code m Change to 2 That's the question 70. Climbing stairs can AC The code of .
322. Change for
The problem is to ask for the minimum number of coins , It doesn't matter whether the coins are combined or arranged ! So the two one. for The order of the cycle can be any way !
// Version of a
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++) {
// Traverse the items
for (int j = coins[i]; j <= amount; j++) {
// Traverse the backpack
if (dp[j - coins[i]] != INT_MAX) {
// If dp[j - coins[i]] If it is the initial value, skip
dp[j] = min(dp[j - coins[i]] + 1, dp[j]);
}
}
}
if (dp[amount] == INT_MAX) return -1;
return dp[amount];
}
};
For the traversal mode, the traversal knapsack is placed in the outer loop , It's OK to traverse items and put them in inner circulation , I'll just give the code
// Version of a
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++) {
// Traverse the items
for (int j = coins[i]; j <= amount; j++) {
// Traverse the backpack
if (dp[j - coins[i]] != INT_MAX) {
// If dp[j - coins[i]] If it is the initial value, skip
dp[j] = min(dp[j - coins[i]] + 1, dp[j]);
}
}
}
if (dp[amount] == INT_MAX) return -1;
return dp[amount];
}
};
边栏推荐
- 遥感影像识别-成像合成
- 安全帽反光衣检测识别数据集和yolov5模型
- Kaggle调用自定义模块方法
- QGIS系列(1)-QGIS(server-apache) win10安装
- 1 semi automatic crawler
- [first song] Introduction to data science of rebirth -- return to advanced level
- C thread lock
- 发布 分辨率0.22m的建筑物分割数据库
- 非真实感渲染(NPR)论文理解及其复现(Unity) - 《Stylized Highlights for Cartoon Rendering and Animation》
- UnityShader-LowPoly
猜你喜欢
随机推荐
力扣题解 二叉树(6)
基于C#的Winform对Access数据库进行操作(mdb结尾)
PZK学C语言之字符串函数(一)
UnityShader-高斯模糊
力扣题解 二叉树(5)
Solve binary tree (7)
Cesium教程 (1) 界面介绍-3dtiles加载-更改鼠标操作设置
[song] rebirth of me in py introductory training (10): numpy
数据库索引的一些说明以及使用
arcgis for js api-入门系列
Stm32-fsmc extended memory SRAM
编程学习记录——第8课【数组与设计五子棋,扫雷游戏】
acwing每日一题 正方形数组的数目
[first song] rebirth of me in py introductory training (6): definition and application of functions
【第一篇博客-展望】
非真实感渲染(NPR)论文理解及其复现(Unity) - 《Stylized Highlights for Cartoon Rendering and Animation》
SQL novice
[first song] rebirth of me in py introductory training (2): formula programming
STM32-FSMC外扩内存SRAM
Redis在windows下的idea连接不上问题








