We have a collection of stones, each stone has a positive integer weight.
Each turn, we choose the two heaviest 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 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 the weight of this stone (or 0 if there are no stones left.)
Note:
1 <= stones.length <= 30
1 <= stones[i] <= 1000
我們有一堆石頭,每顆石頭都有一個正整數代表它的重量。
每一回合,我們選兩顆最大的石頭並讓它們相撞。假設那兩顆石頭的重量分別為x和y且x <= y,那麼相撞後的結果會是:
如果x == y,兩顆石頭都完全粉碎。
如果x != y,重量為x的石頭會完全粉碎,而重量為y的石頭重量會改為y - x。
最後只剩下一顆石頭時,回傳那顆石頭的重量(如果沒有剩下石頭就回傳0)。
提示:
1 <= stones.length <= 30
1 <= stones[i] <= 1000
LeetCode
C++