๐ 1049. ๆๅไธๅ็ณๅคด็้้ II
2022ๅนด6ๆ9ๆฅๅฐไบ 1 ๅ้
๐ 1049. ๆๅไธๅ็ณๅคด็้้ II
้พๅบฆ: ๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class Solution {
public int lastStoneWeightII(int[] stones) {
// ๆ่ทฏ๏ผ
// ่ๅ
้ฎ้ข๏ผๆฑ่ฝๅพๅฐๆๅคงไปทๅผไธ่ฌ็่ฝๅพๅฐ็ๆๅคงไปทๅผ๏ผๅไธบไธ็ป
int sum = 0;
for(int i : stones) {
sum += i;
}
int res = sum;
sum >>= 1;
// ๅกซๅ
่ๅ
๏ผไฝฟๅพไปทๅผๅฐฝๅฏ่ฝๆฅ่ฟๆๅคงไปทๅผ็ไธๅ
int len = stones.length;
int[] dp = new int[sum + 1];
for(int j = 0; j < len; j ++) {
for(int i = sum; i - stones[j] >= 0; i --) {
dp[i] = Math.max(dp[i], dp[i - stones[j]] + stones[j]);
}
}
return Math.abs(2 * dp[sum] - res);
}
}