当前位置:网站首页>Dynamic programming - 01 knapsack problem
Dynamic programming - 01 knapsack problem
2022-07-19 02:42:00 【Little sun who wants to be a program yuan】
The problem background :
The thief has a capacity of W The backpack , Yes n Items , The first i Item value vi, weight wi.
The goal is : find xi Such that for all xi = {0,1}, bring sum(xi * wi) <= W And sum(xi * vi) Maximum .
Problem solving :
Solution 1 ( Violence goes back ):
public class Package01 {
public static int search(int W, int[] v, int[] w, int index) {
if(index < 0)
return 0;
int a = W >= w[index]? search(W - w[index], v, w, index - 1) + v[index] : 0;
int b = search(W, v, w, index - 1);
return Math.max(a, b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] v = {50,200,180,225,200};
int[] w = {5,25,30,45,50};
int ans = search(100, v, w, 4);
System.out.println(ans);
}
}Solution 2 :
Analyze redundancy : In both cases f(n - 1) --- f(n - 1, w) & f(n - 1, w') ---> Cache with a two-dimensional array
public class Package01 {
static int[][] f = new int[5][101];
public static int search(int W, int[] v, int[] w, int index) {
if(index < 0)
return 0;
if(f[index][W] > 0)
return f[index][W];
int a = W >= w[index]? search(W - w[index], v, w, index - 1) + v[index] : 0;
int b = search(W, v, w, index - 1);
f[index][W] = Math.max(a, b);
return f[index][W];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] v = {50,200,180,225,200};
int[] w = {5,25,30,45,50};
int ans = search(100, v, w, 4);
System.out.println(ans);
}
}limitations :
a. If W It's a double Type of , This caching method cannot be used
b. If W When it's worth a lot , You can't use this method
Solution 3 ( Recurrence ):
public class Package01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] v = {50,200,180,225,200};
int[] w = {5,25,30,45,50};
int W = 100;
int[][] f = new int[v.length][W + 1];
for(int i = 0; i < v.length; ++i) {
for(int j = 0; j <= W; ++j) {
if(i == 0) {
f[i][j] = j >= w[i]? v[i] : 0;
}
else {
int a = j >= w[i]? f[i - 1][j - w[i]] + v[i] : 0;
int b = f[i - 1][j];
f[i][j] = Math.max(a, b);
}
}
}
System.out.println(f[v.length - 1][W]);
}
}summary :
Dynamic programming is a search algorithm without repetition and omission .
Spatial complexity :n*W
Time complexity :O(n*W)
expand :
If W Particularly large , The space complexity will be very high
Improve the code :
Ideas :f[i] It only needs f[i - 1], Unwanted f[i - 2]; new f[i], delete f[i - 2]
Handle : It can be used %2 The operation of ( Scrolling array )
public class Package01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] v = {50,200,180,225,200};
int[] w = {5,25,30,45,50};
int W = 100;
int[][] f = new int[2][W + 1];
for(int i = 0; i < v.length; ++i) {
for(int j = 0; j <= W; ++j) {
//f[i] It only needs f[i - 1], Unwanted f[i - 2]
//new f[i], delete f[i - 2]
f[i % 2][j] = 0;
if(i == 0) {
f[i % 2][j] = j >= w[i]? v[i] : 0;
}
else {
int a = j >= w[i]? f[(i - 1) % 2][j - w[i]] + v[i] : 0;
int b = f[(i - 1) % 2][j];
f[i % 2][j] = Math.max(a, b);
}
}
}
System.out.println(f[(v.length - 1) % 2][W]);
}
}
边栏推荐
- shell脚本之条件语句
- [antv G2] how to add a click event to the line chart (click anywhere to get the value of the point on the line)
- Post man JSON script to JMX script of JMeter
- shell脚本接收和返回参数
- D - Parity game离散化+带权并查集
- PowerStor500T报错0x01806803
- Uniapp wechat applet login (authorize wechat first and then mobile phone number) - (1)
- The solution to the bounce and offset of unity3d game characters when jumping to the ground
- 全链路压测
- Tree array and St table
猜你喜欢

Static routing (detailed)

Firewalld 防火墙

Cocoon breaking and rebirth of 3D NFT: caduceus decentralized edge rendering technology

逆元(名字太多人用我就加这几个字)

Network layer transmission protocol (detailed)

Network layer protocol and IP packet format (detailed)

echo -e用法

CTFHub----RCE

Traversal of binary tree

In depth performance test data analysis
随机推荐
Flyway的SaaS多租户实现方案
西加加
逆元(名字太多人用我就加这几个字)
Test points of login function
正则、扩展表达式,sed文本处理器与awk工具、用脚本改IP地址
Jstat命令查看jvm的GC情况
[unity Editor Extension] find all objects of a script attached in the scene and resources
Subnet division (see details)
The difference between cookies and sessions
Uniapp wechat applet login (authorize wechat first and then mobile phone number) - (1)
squid代理服务部署
解决WIN10连接共享打印机出现0x00000709的错误
Gzip的动态压缩和静态压缩详解
Simple use case writing specification
In depth performance test data analysis
GoReplay
脏读、幻读、不可重复读
Leetcode 70:Climbing Stairs
[hsjframework] unity time management timemanger timer
Longest ascending subsequence - Optimization