> For the complete documentation index, see [llms.txt](https://junnie.gitbook.io/nine-chapter/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://junnie.gitbook.io/nine-chapter/9.dynamic-programming/92backpack.md).

# Backpack I\~VI

## 1.(92)Backpack I(单次选择+最大体积)

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**](http://lintcode.com/en/problem/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.

## Code

动规经典题目，用数组dp\[i]表示书包空间为i的时候能装的A物品最大容量。两次循环，外部遍历数组A，内部反向遍历数组dp，若j即背包容量大于等于物品体积A\[i]，则取前i-1次循环求得的最大容量dp\[j]，和背包体积为j-A\[i]时的最大容量dp\[j-A\[i]]与第i个物品体积A\[i]之和即dp\[j-A\[i]]+A\[i]的较大值，作为本次循环后的最大容量dp\[i]。

注意dp\[]的空间要给m+1，因为我们要求的是第m+1个值dp\[m]，否则会抛出OutOfBoundException。

```
public int backPack(int m, int[] A) {

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

## 2.(125)Backpack II (单次选择+最大价值)

Given*n\_items with size Aiand value Vi, 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**](http://lintcode.com/en/problem/backpack-ii/#challenge)

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

## Code

和BackPack I基本一致。依然是以背包空间为限制条件，所不同的是dp\[j]取的是价值较大值，而非体积较大值。所以只要把dp\[j-A\[i]]+A\[i]换成dp\[j-A\[i]]+V\[i]就可以了。

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://junnie.gitbook.io/nine-chapter/9.dynamic-programming/92backpack.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
