# 1049. Last Stone Weight II

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MkNAFE0IXYptbyVLAEe%2F-MkSBhkzvRVjiVgZwwSE%2Fimage.png?alt=media\&token=c43123d6-a02d-4811-a380-56d1aa976566)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MkNAFE0IXYptbyVLAEe%2F-MkSBnVQIZOG36kbZxXv%2Fimage.png?alt=media\&token=58c1cdb4-20d3-44b9-9455-52a40df340c6)

## same as leetcode 416, but this one caculate the difference.

time: O(n\*m)

space: O(n\*m)

```java
class Solution {
    public int lastStoneWeightII(int[] stones) {
        int sum = 0;
        for (int num : stones) { // add them all
            sum += num;
        }
        
        int n = stones.length;
        int m = sum/2; // become 01 backpack problems, target is sum/2
        
        boolean dp[][] = new boolean[n+1][m+1];
        
        int min = Integer.MAX_VALUE;
        dp[0][0] = true;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                if (j >= stones[i-1]) {
                    dp[i][j] = dp[i-1][j] || dp[i-1][j - stones[i-1]];
                } else {
                    dp[i][j] = dp[i-1][j];
                }
                
                if (dp[i][j]) { // if can fit to sum/2 pack, caculate the diff, the current weight size is j
                    int diff = Math.abs(sum - j*2);
                    min = Math.min(min, diff);
                }
            }
        }
        return min; 
}
```

## second solution

<https://leetcode.com/problems/last-stone-weight-ii/discuss/294888/JavaC%2B%2BPython-Easy-Knapsacks-DP>

```java
class Solution {
    public int lastStoneWeightII(int[] stones) {
        int sum = 0;
        for (int num : stones) { // add them all
            sum += num;
        }
        
        int n = stones.length;
        int m = sum/2; // become 01 backpack problems, target is sum/2
        
        int dp[][] = new int[n+1][m+1];
        
        dp[0][0] = 0;
        
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                if (j >= stones[i-1]) {
                    dp[i][j] = Math.max(
                        dp[i-1][j], 
                        dp[i-1][j - stones[i-1]] + stones[i-1]
                    );
                } else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return sum - 2*dp[n][m]; // at last, dp[n][m] fit to sum/2's result, so *2
    }
}
```


---

# Agent Instructions: 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://timmybeeflin.gitbook.io/cracking-leetcode/dynamic-programming/01-backpack/untitled.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.
