Last Stone Weight II

Given an array of stone weights, repeatedly smash the two heaviest stones together and return the minimum possible final stone weight. This problem reduces to splitting the stones into two subsets with minimum difference.

Input Format

stones = array of stone weights

Output Format

minimum possible final stone weight

Constraints

  • 1 <= stones.length <= 30; 1 <= stones[i] <= 100

Examples

Example 1:

Input:

stones = [2,7,4,1,8,1]

Output:

1

Explanation:

A near-even partition leaves a stone of weight 1.

Example 2:

Input:

stones = [31,26,33,21,40]

Output:

5

Explanation:

The best achievable difference is 5.

Loading...
Last Stone Weight II - Dp Knapsack DSA Problem