> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Last Stone Weight II Python Solution

> Tested Python solution for LeetCode 1049 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1049, Medium. Topics: Array, Dynamic Programming. [View on LeetCode](https://leetcode.com/problems/last-stone-weight-ii/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1049   # by problem number
lcpy gen -s last_stone_weight_ii   # by problem name
```

## Problem

You are given an array of integers `stones` where `stones[i]` is the weight of the `i^th` stone.

We are playing a game with the stones. On each turn, we choose any two stones and smash them together. Suppose the stones have weights `x` and `y` with `x <= y`. The result of this smash is:

* If `x == y`, both stones are destroyed, and
* If `x != y`, the stone of weight `x` is destroyed, and the stone of weight `y` has new weight `y - x`.

At the end of the game, there is **at most** one stone left.

Return *the smallest possible weight of the left stone*. If there are no stones left, return `0`.

### Examples

```
Input: stones = [2,7,4,1,8,1]
Output: 1
Explanation:
We can combine 2 and 4 to get 2, so the array converts to [2,7,1,8,1] then,
we can combine 7 and 8 to get 1, so the array converts to [2,1,1,1] then,
we can combine 2 and 1 to get 1, so the array converts to [1,1,1] then,
we can combine 1 and 1 to get 0, so the array converts to [1], then that's the optimal value.
```

```
Input: stones = [31,26,33,21,40]
Output: 5
```

### Constraints

* 1 \<= stones.length \<= 30
* 1 \<= stones\[i] \<= 100

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/last_stone_weight_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/last_stone_weight_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * total)
    # Space: O(total)
    def last_stone_weight_ii(self, stones: list[int]) -> int:
        total = sum(stones)
        target = total // 2

        # reachable[s] = True if a subset sums to s.
        reachable = [False] * (target + 1)
        reachable[0] = True

        for stone in stones:
            for s in range(target, stone - 1, -1):
                if reachable[s - stone]:
                    reachable[s] = True

        for s in range(target, -1, -1):
            if reachable[s]:
                return total - 2 * s
        return total
```

## Complexity

| Time          | Space    |
| ------------- | -------- |
| O(n \* total) | O(total) |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
