|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author Openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/longest-string-chain "Longest String Chain") |
| 9 | + |
| 10 | +Next > |
| 11 | + |
| 12 | +## 1049. Last Stone Weight II (Medium) |
| 13 | + |
| 14 | +<p>We have a collection of rocks, each rock has a positive integer weight.</p> |
| 15 | + |
| 16 | +<p>Each turn, we choose <strong>any two rocks</strong> and smash them together. Suppose the stones have weights <code>x</code> and <code>y</code> with <code>x <= y</code>. The result of this smash is:</p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li>If <code>x == y</code>, both stones are totally destroyed;</li> |
| 20 | + <li>If <code>x != y</code>, the stone of weight <code>x</code> is totally destroyed, and the stone of weight <code>y</code> has new weight <code>y-x</code>.</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p>At the end, there is at most 1 stone left. Return the <strong>smallest possible</strong> weight of this stone (the weight is 0 if there are no stones left.)</p> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | + |
| 27 | +<p><strong>Example 1:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input: </strong>[2,7,4,1,8,1] |
| 31 | +<strong>Output: </strong>1 |
| 32 | +<strong>Explanation: </strong> |
| 33 | +We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then, |
| 34 | +we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then, |
| 35 | +we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then, |
| 36 | +we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value. |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | + |
| 41 | +<p><strong>Note:</strong></p> |
| 42 | + |
| 43 | +<ol> |
| 44 | + <li><code>1 <= stones.length <= 30</code></li> |
| 45 | + <li><code>1 <= stones[i] <= 100</code></li> |
| 46 | +</ol> |
0 commit comments