1046. Last Stone Weight
https://leetcode.com/problems/last-stone-weight/
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 weightx
is totally destroyed, and the stone of weighty
has new weighty-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.)
Example 1:
Note:
1 <= stones.length <= 30
1 <= stones[i] <= 1000
由正整数构成的数组,每次选值最大的两个元素让它们减去它们中的最小值后把大于0的插回数组,问最后如果剩下一个元素它的值会是多少。动态找最小元素,并能动态更新 => heap。
Last updated
Was this helpful?