# [LintCode] Backpack I II III IV V VI [背包六问]

## Backpack I

### Problem 单次选择+最大体积

Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack?

### Notice

You can not divide any item into small pieces.

### Example

If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can select [2, 3, 5], so that the max size we can fill this backpack is 10. If the backpack size is 12. we can select [2, 3, 7] so that we can fulfill the backpack.

You function should return the max size we can fill in the given backpack.

### Challenge

O(n x m) time and O(m) memory.

O(n x m) memory is also acceptable if you do not know how to optimize memory.

### Solution

``````public class Solution {
public int backPack(int m, int[] A) {
int[] dp = new int[m+1];
for (int i = 0; i < A.length; i++) {
for (int j = m; j > 0; j--) {
if (j >= A[i]) {
dp[j] = Math.max(dp[j], dp[j-A[i]] + A[i]);
}
}
}
return dp[m];
}
}``````

## Backpack II

### Problem 单次选择+最大价值

Given n items with size A[i] and value V[i], and a backpack with size m. What's the maximum value can you put into the backpack?

### Notice

You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.

### Example

Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 9.

### Challenge

O(n x m) memory is acceptable, can you do it in O(m) memory?

### Solution

``````public class Solution {
public int backPackII(int m, int[] A, int V[]) {
int[] dp = new int[m+1];
for (int i = 0; i < A.length; i++) {
for (int j = m; j > 0; j--) {
if (j >= A[i]) dp[j] = Math.max(dp[j], dp[j-A[i]]+V[i]);
}
}
return dp[m];
}
}``````

## Backpack III

### Problem 重复选择+最大价值

Given n kind of items with size Ai and value Vi( each item has an infinite number available) and a backpack with size m. What's the maximum value can you put into the backpack?

### Notice

You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.

### Example

Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 15.

### Solution

``````public class Solution {
public int backPackIII(int[] A, int[] V, int m) {
int[] dp = new int[m+1];
for (int i = 0; i < A.length; i++) {
for (int j = 1; j <= m; j++) {
if (j >= A[i]) dp[j] = Math.max(dp[j], dp[j-A[i]]+V[i]);
}
}
return dp[m];
}
}``````

## Backpack IV

### Problem 重复选择+唯一排列+装满可能性总数

Given n items with size nums[i] which an integer array and all positive numbers, no duplicates. An integer target denotes the size of a backpack. Find the number of possible fill the backpack.

Each item may be chosen unlimited number of times

### Example

Given candidate items [2,3,6,7] and target 7,

A solution set is:

``````[7]
[2, 2, 3]
return 2
``````

### Solution

``````public class Solution {
public int backPackIV(int[] nums, int target) {
int[] dp = new int[target+1];
dp[0] = 1;
for (int i = 0; i < nums.length; i++) {
for (int j = 1; j <= target; j++) {
if (nums[i] == j) dp[j]++;
else if (nums[i] < j) dp[j] += dp[j-nums[i]];
}
}
return dp[target];
}
}``````

## Backpack V

### Problem 单次选择+装满可能性总数

Given n items with size nums[i] which an integer array and all positive numbers. An integer target denotes the size of a backpack. Find the number of possible fill the backpack.

Each item may only be used once

### Example

Given candidate items [1,2,3,3,7] and target 7,

A solution set is:

``````[7]
[1, 3, 3]
return 2
``````

### Solution

``````public class Solution {
public int backPackV(int[] nums, int target) {
int[] dp = new int[target+1];
dp[0] = 1;
for (int i = 0; i < nums.length; i++) {
for (int j = target; j >= 0; j--) {
if (j >= nums[i]) dp[j] += dp[j-nums[i]];
}
}
return dp[target];
}
}``````

## Backpack VI aka: Combination Sum IV

### Problem 重复选择+不同排列+装满可能性总数

Given an integer array nums with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

### Notice

The different sequences are counted as different combinations.

### Example

Given nums = [1, 2, 4], target = 4

The possible combination ways are:

``````[1, 1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[2, 1, 1]
[2, 2]
[4]
return 6
``````

### Solution

``````public class Solution {
public int backPackVI(int[] nums, int target) {
int[] dp = new int[target+1];
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (int num: nums) {
if (num <= i) dp[i] += dp[i-num];
}
}
return dp[target];
}
}``````

1 篇内容引用
0 条评论