当前位置:网站首页>Brute force recursion to dynamic programming 03 (knapsack problem)
Brute force recursion to dynamic programming 03 (knapsack problem)
2022-07-29 23:21:00 【Taotao can't learn English】
1. 题目
有n个物品,它们有各自的体积和价值,现有给定容量的背包,如何让背包里装入的物品具有最大的价值总和?
2. 暴力递归
如果剩余空间<0,则返回-1
If choose the last one,Choose directly back to0
对于一个物品,可以选,可以不选
如果不选,curIndex+1,This item just skip
如果选,Figure out whether returns to the value of the -1 ,如果不是,The add new value
public int getMaxValue(int[] weight, int[] value, int bagSize) {
return getValue(weight, value, bagSize, 0);
}
/** * 从左往右 01背包 * * @param weight 物品重量数组 * @param value 物品价值数组 * @param bagFreeSpace Pack the remaining space * @param curIndex * @return */
private int getValue(int[] weight, int[] value, int bagFreeSpace, int curIndex) {
//如果剩余空间<0
if (bagFreeSpace < 0) {
return -1;
}
if (curIndex == value.length) {
//If choose the last one value[value.length - 1]
return 0;
}
//If the current items don't choose,Directly under the selected one
int unSelect = getValue(weight, value, bagFreeSpace, curIndex + 1);
//If the current item can choose
int select = 0;
int next = getValue(weight, value, bagFreeSpace - weight[curIndex], curIndex + 1);
if (next != -1) {
select = value[curIndex] + next;
}
//选择最大价值
return Math.max(unSelect, select);
}
2. 动态规划
dp数组,Lateral to select items,Vertical to the current volume
By a recursive process of return the,Elected all items,即 curIndex == n , 则dp[ n ] [ curIndex] = 0
Recursive from down to up (第 n - 1 行开始,第 n 行全部为0),From left to right circulation
如果不选,为dp[i+1][j]
如果选,value[i] + dp[i + 1][j - weight[i]]
In even the maximum
public int getMaxValue2(int[] weight, int[] value, int bagSize) {
int n = weight.length;
//Lateral to pack the remaining space
//Longitudinal to the currently selected item
int[][] dp = new int[n + 1][bagSize + 1];
//Recursive knowable by violence,当curIndex==n , 则为0
// Java 默认为0
for (int i = 0; i <=bagSize; i++) {
dp[n][i] = 0;
}
//物品
for (int i = n-1; i >= 0; i--) {
for (int j = 0; j <= bagSize; j++) {
//If the current items don't choose,Directly under the selected one
int unSelect =dp[i+1][j];
//If the current item can choose
int select = 0;
if (j >= weight[i]) {
select = value[i] + dp[i + 1][j - weight[i]];
}
dp[i][j] = Math.max(unSelect, select);
}
}
return dp[0][bagSize];
}
public static void main(String[] args) {
int[] weights = {
3, 2, 4, 7};
int[] values = {
5, 6, 3, 19};
int bag = 11;
System.out.println(new Package().getMaxValue(weights, values, bag));
System.out.println(new Package().getMaxValue2(weights, values, bag));
}
边栏推荐
- BGP Federal Comprehensive Experiment
- kaniko --customPlatform参数:支持不同平台的镜像构建(如:arm等)
- MySQL Interview Questions: Detailed Explanation of User Amount Recharge Interview Questions
- Access the company intranet
- 【MySQL系列】 MySQL表的增删改查(进阶)
- 暴力递归到动态规划 04 (数字字符串转化)
- Topics in Dynamic Programming
- 什么是色选机(color sorter)?
- PyCharm使用教程(详细版 - 图文结合)
- Elementary C language - first understanding of C language
猜你喜欢

【leetcode】75. 颜色分类(中等)(双指针、原地修改)

Super RVRT

The latest Gansu construction welder (construction special operation) simulation question bank and answer analysis in 2022

流水线上的农民:我在工厂种蔬菜

浅析即时通讯移动端开发DNS域名劫持等杂症

【MySQL系列】 MySQL表的增删改查(进阶)

《MySQL DBA封神打怪之路》专栏学习大纲

Raspberry pie wiringPi 2.6 installed on solving gpio readall command mistakes

【微信小程序】一文解忧,事件绑定

Access Modbus TCP and Modbus RTU protocol devices using Neuron
随机推荐
The Sandbox 与 Gravity 达成合作,将《RO仙境传说》带入元宇宙
我们上线了一个「开发者实验室」
Professor Lu Shouqun from COPU was invited to give a speech at ApacheCon Asia
一个print函数,挺会玩啊?
Qt之在QML中使用QSortFilterProxyModel进行排序和过滤
暴力递归到动态规划 04 (数字字符串转化)
[leetcode] 82. Delete duplicate elements in sorted linked list II (medium)
@Accessors 注解详解
MQTT over QUIC: The Next-Generation IoT Standard Protocol Brings New Impetus to Messaging Scenarios
Analysis of miscellaneous diseases such as DNS domain name hijacking in instant messaging mobile terminal development
How to realize object selection in canvas (5)
Another new rule for credit cards is coming!Juphoon uses technology to boost the financial industry to improve service quality and efficiency
JetsonNano学习(六)Jetson踩过的大坑及解决方法___持续更新
Any to Any 实时变声的实现与落地丨RTC Dev Meetup
The sequence table of the linear table (the dry goods are full of sharing ~ contains all the function codes of the sequence table~
浅析即时通讯移动端开发DNS域名劫持等杂症
使用 Neuron 接入 Modbus TCP 及 Modbus RTU 协议设备
【2023校招刷题】常见面试问题总结(七、常见总线协议篇)(随后续面试不断更新....)
《MySQL DBA封神打怪之路》专栏学习大纲
Guidelines for the Release of New WeChat Mini Programs