纯 0 - 1 背包 (opens new window)(动态规划:01背包理论基础(滚动数组))是求 给定背包容量 装满背包 的最大价值是多少。
package 代码随想录.动态规划.小总结240411;
import java.util.Scanner;
public class _01背包理论基础_滚动数组 {
public static void main(String[] args) {
// TODO 假设物品种类为M,背包容量为N
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt();
int N = scanner.nextInt();
//对物品赋予其对应该有的价值与重量
int values [] = new int [M];
int weights [] = new int [M];
//给每一个重量与价值赋初始值
for (int i = 0; i < M;i++){
weights[i] = scanner.nextInt();
}
for (int i = 0;i<M;i++){
values[i] = scanner.nextInt();
}
int dp [][] = new int[M][N+1];
//初始化dp数组
for (int i = weights[0];i<=N;i++){
dp[0][i] = values[0];
}
//先遍历物品
for (int i = 1;i<M;i++){
//再遍历背包
for (int j = 0;j<=N;j++){
if (weights[i] > j){
dp[i][j] = dp[i-1][j]; //能背下,则考虑背上去的结果,存储到dp数组中。
}else {
dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-weights[i]] + values[i]);
}
}
}
System.out.println(dp[M-1][N]);
}
}
416. 分割等和子集 (opens new window)是求 给定背包容量,能不能装满这个背包。
package 代码随想录.动态规划.小总结240411;
import java.util.Arrays;
public class _416分割等和子集_dp {
/**
*
* @param nums
* @return
*/
public boolean canPartition(int[] nums) {
// TODO 请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。
int len = nums.length;
if (len == 1){
return false;
}
int allSum = Arrays.stream(nums).sum();
if (allSum % 2 != 0) {
return false;
}
Arrays.sort(nums);
int target = allSum / 2;
//初始化dp数组
int dp [][] = new int[nums.length][target + 1];
//初始化dp数组元素
for(int j = 0; j <= target; j++){
if(j < nums[0]){
dp[0][j] = 0;
}
else{
dp[0][j] = nums[0];
}
}
// for(int j = 0; j <= target; j++){
// dp[0][j] = nums[0];
// }
//开始动态规划一下
for (int i = 1;i<len;i++){
for (int j = 0;j<=target;j++){
if (j < nums[i]){
dp[i][j] = dp[i-1][j];
}else {
dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j - nums[i]] + nums[i]);
}
}
}
//打印出来给你自己看看
// for(int x : dp){
// System.out.print(x + ",");
// }
// System.out.print(" "+i+" row"+"\n");
return dp[len-1][target] == target;
}
}
1049.最后一块石头的重量 II (opens new window)是求 给定背包容量,尽可能装,最多能装多少.
package 代码随想录.动态规划;
import java.util.Arrays;
public class _1049最后一块石头的重量II_dp {
/**
*
* @param stones
* @return
*/
public int lastStoneWeightII(int[] stones) {
int allSum = 0;
allSum = Arrays.stream(stones).sum();
int targetHalf = allSum / 2;
//初始化,dp[i][j]为可以放0-i物品,背包容量为j的情况下背包中的最大价值
int [][] dp = new int[stones.length][targetHalf+1];
//dp[i][0]默认初始化为0
//dp[0][j]取决于stones[0]
for (int j = stones[0];j<=targetHalf;j++){
dp[0][j] = stones[0];
}
for (int i = 1;i<stones.length;i++){
for (int j =1;j<=targetHalf;j++){ //范围为+1,因此可以取到等号
if (j >= stones[i]){
//不放:dp[i - 1][j] 放:dp[i - 1][j - stones[i]] + stones[i]
dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-stones[i]] + stones[i]);
}else {
dp[i][j] = dp[i-1][j];
}
}
}
System.out.println(dp[stones.length - 1][targetHalf]);
return (allSum - dp[stones.length-1][targetHalf]) - dp[stones.length - 1][targetHalf];
}
}
1049. 目标和 (opens new window)是求 给定背包容量,装满背包有多少种方法。
本题是求 给定背包容量,装满背包最多有多少个物品。
package 代码随想录.动态规划;
import java.util.Arrays;
public class _1049最后一块石头的重量II_一维空间压缩 {
/**
* 每次都去找最小差
*
* @param stones
* @return
*/
public int lastStoneWeightII(int[] stones) {
int sum = 0;
sum = Arrays.stream(stones).sum();
int target = sum >> 1;
//初始化dp数组
int [] dp = new int[target + 1];
for (int i = 0; i < stones.length; i++) {
//采用倒序
for (int j = target; j >= stones[i];j--){
//两种情况,要么放,要么不放
dp[j] = Math.max(dp[j],dp[j - stones[i]] + stones[i]);
}
}
return sum - 2*dp[target];
}
}
package 代码随想录.动态规划.小总结240411;
public class _474一和零_dp {
/**
*
* @param strs
* @param m
* @param n
* @return
*/
public int findMaxForm(String[] strs, int m, int n) {
int [][] dp = new int[m+1][n+1];
int length = strs.length;
for (int i = 0; i < length; i++) {
int [] zerosAndOnes = getNumsOfZerosAndOnes(strs[i]);
int zeros = zerosAndOnes[0],ones = zerosAndOnes[1];
for (int j = m; j >= zeros; j--) {
for (int k = n; k >= ones; k--){
dp[j][k] = Math.max(dp[j][k],dp[j-zeros][k-ones]+1);
}
}
}
return dp[m][n];
}
private int[] getNumsOfZerosAndOnes(String str) {
int [] zerosOnes = new int[2];
int length = str.length();
for (int i = 0;i<length;i++){
zerosOnes[str.charAt(i) - '0']++;
}
return zerosOnes;
}
}
package 代码随想录.动态规划.小总结240411;
public class _474一和零_三维dp {
/**
*
* @param strs
* @param m
* @param n
* @return
*/
public int findMaxForm(String[] strs, int m, int n) {
int length= strs.length;
int [][][] dp = new int[length+1][m+1][n+1];
for (int i=1; i<=length; i++){
int [] zerosAndOnes= getNumsOfZerosAndOnes(strs[i-1]);
int zeros = zerosAndOnes[0],ones = zerosAndOnes[1];
for (int j = 0;j<=m;j++){
for (int k = 0;k<=n;k++){
dp[i][j][k] = dp[i-1][j][k];
if (j >=zeros && k>=ones){
dp[i][j][k] = Math.max(dp[i][j][k],dp[i-1][j - zeros][k-ones]+1);
}
}
}
}
return dp[length][m][n];
}
private int[] getNumsOfZerosAndOnes(String str) {
int [] zerosOnes = new int[2];
int length = str.length();
for (int i = 0;i<length;i++){
zerosOnes[str.charAt(i) - '0']++;
}
return zerosOnes;
}
}
package 代码随想录.动态规划.小总结240411;
public class _474一和零_三维dp {
/**
*
* @param strs
* @param m
* @param n
* @return
*/
public int findMaxForm(String[] strs, int m, int n) {
int length= strs.length;
int [][][] dp = new int[length+1][m+1][n+1];
for (int i=1; i<=length; i++){
int [] zerosAndOnes= getNumsOfZerosAndOnes(strs[i-1]);
int zeros = zerosAndOnes[0],ones = zerosAndOnes[1];
for (int j = 0;j<=m;j++){
for (int k = 0;k<=n;k++){
dp[i][j][k] = dp[i-1][j][k];
if (j >=zeros && k>=ones){
dp[i][j][k] = Math.max(dp[i][j][k],dp[i-1][j - zeros][k-ones]+1);
}
}
}
}
return dp[length][m][n];
}
private int[] getNumsOfZerosAndOnes(String str) {
int [] zerosOnes = new int[2];
int length = str.length();
for (int i = 0;i<length;i++){
zerosOnes[str.charAt(i) - '0']++;
}
return zerosOnes;
}
}