Last Stone Weight II
We have a collection of rocks, each rock has a positive integer weight.
Each turn, we chooseany two rocks and smash them together. Suppose the stones have weightsx
andy
withx <= y
. The result of this smash is:
If
x == y
, both stones are totally destroyed;
If
x != y
, the stone of weight
x
is totally destroyed, and the stone of weight
y
has new weight
y-x
.
At the end, there is at most 1 stone left. Return thesmallest possibleweight of this stone (the weight is 0 if there are no stones left.)
Example 1:
Note:
1
<
= stones.length
<
= 30
1
<
= stones[i]
<
= 100
分析
其实就是分成2个尽量大的组,2个sum的diff
看成01背包,分成2个尽量等分背包d。最后就是sum-2*d.2个等分抵消
记住这里dp[v]是bool,表示该数是否可以到达,最后结果是sum-2*v 开始写成sum-2*dp[v ]错半死
本题也可以看成2个group A,B ,2个组和的diff, res=abs( A-B)。 所以当前元素入A就+ 入B就-。最后取正数
记住set的相加就是|
Last updated