Skip to content

Commit fbb5991

Browse files
Merge pull request youngyangyang04#755 from kok-s0s/master
提供JavaScript版本代码
2 parents a4465c0 + 5257958 commit fbb5991

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

problems/1049.最后一块石头的重量II.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,28 @@ func max(a, b int) int {
219219
}
220220
```
221221

222+
JavaScript版本
223+
224+
```javascript
225+
/**
226+
* @param {number[]} stones
227+
* @return {number}
228+
*/
229+
var lastStoneWeightII = function (stones) {
230+
let sum = stones.reduce((s, n) => s + n);
231+
232+
let dpLen = Math.floor(sum / 2);
233+
let dp = new Array(dpLen + 1).fill(0);
234+
235+
for (let i = 0; i < stones.length; ++i) {
236+
for (let j = dpLen; j >= stones[i]; --j) {
237+
dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
238+
}
239+
}
222240

241+
return sum - dp[dpLen] - dp[dpLen];
242+
};
243+
```
223244

224245
-----------------------
225246
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

0 commit comments

Comments
 (0)