You are given an array of integers stones where stones[i] is the weight of the ith 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.
Input: stones =[2,7,4,1,8,1]Output: 1Explanation:
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.
The problem can be transformed into partitioning the stones into two groups such that the difference of their sums is minimized. This is a classic subset sum problem, similar to partitioning an array into two subsets with the minimum difference.
classSolution {
publicintlastStoneWeightII(int[] stones) {
int sum = 0;
for (int s : stones) sum += s;
int target = sum / 2;
boolean[] dp =newboolean[target + 1];
dp[0]=true;
for (int s : stones) {
for (int j = target; j >= s; --j) {
dp[j]= dp[j]|| dp[j - s];
}
}
for (int j = target; j >= 0; --j) {
if (dp[j]) return sum - 2 * j;
}
return 0;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funlastStoneWeightII(stones: IntArray): Int {
val sum = stones.sum()
val target = sum / 2val dp = BooleanArray(target + 1)
dp[0] = truefor (s in stones) {
for (j in target downTo s) {
dp[j] = dp[j] || dp[j - s]
}
}
for (j in target downTo 0) {
if (dp[j]) return sum - 2 * j
}
return0 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
deflastStoneWeightII(self, stones: list[int]) -> int:
total = sum(stones)
target = total //2 dp = [False] * (target +1)
dp[0] =Truefor s in stones:
for j in range(target, s -1, -1):
dp[j] = dp[j] or dp[j - s]
for j in range(target, -1, -1):
if dp[j]:
return total -2* j
return0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfnlast_stone_weight_ii(stones: Vec<i32>) -> i32 {
let sum: i32= stones.iter().sum();
let target = sum /2;
letmut dp =vec![false; (target +1) asusize];
dp[0] =true;
for&s in&stones {
for j in (s..=target).rev() {
dp[j asusize] = dp[j asusize] || dp[(j - s) asusize];
}
}
for j in (0..=target).rev() {
if dp[j asusize] {
return sum -2* j;
}
}
0 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
lastStoneWeightII(stones: number[]):number {
constsum=stones.reduce((a, b) =>a+b, 0);
consttarget= Math.floor(sum/2);
constdp= Array(target+1).fill(false);
dp[0] =true;
for (constsofstones) {
for (letj=target; j>=s; --j) {
dp[j] =dp[j] ||dp[j-s];
}
}
for (letj=target; j>=0; --j) {
if (dp[j]) returnsum-2*j;
}
return0;
}
}
⏰ Time complexity: O(n * sum/2), where n is the number of stones and sum is the total weight. Each stone and each possible sum up to half the total are considered.