class Solution {
public int lastStoneWeightII(int[] stones) {
int sum = 0;
for(int s : stones){
sum += s;
}
int target = sum/2;
int[][] dp = new int[stones.length][target+1];
for(int i = stones[0];i<=target;i++){
dp[0][i] = stones[0];
}
for(int i = 1;i<stones.length;i++){
for(int j = 1;j<=target;j++){
if(j >= stones[i]){
dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-stones[i]]+stones[i]);
}
else{
dp[i][j] = dp[i-1][j];
}
}
}
System.out.println(dp[stones.length - 1][target]);
return (sum - dp[stones.length - 1][target]) - dp[stones.length - 1][target];
}
}
代码随想录——1049.最后一块石头的重量II
最新推荐文章于 2024-08-31 15:48:32 发布