<jupyter_start><jupyter_text>SolutionWe 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.)Example 1:```Input: [2,7,4,1,8,1]Output: 1Explanation: We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.```Note:1. 1 <= stones.length <= 302. 1 <= stones[i] <= 1000  Solution<jupyter_code>import heapq
from random import choices
arr = choices(list(range(1,1001)), k=30)
def last_stone_weight(stones: list) -> int:
    heap = []

    # Python doesn't have a maxheap implementation so we need to reverse the values
    for value in arr:
        heapq.heappush(heap, -value)

    while len(heap) > 1:
        x = -heapq.heappop(heap)
        y = -heapq.heappop(heap)

        if x > y:
            heapq.heappush(heap, -(x-y))
    
    if heap:
        return -heap[0]
    return 0
%time last_stone_weight(arr)
def last_stone_weight(stones: list) -> int: 
    while len(stones) > 1:
        x = max(stones)
        stones.remove(x)
        
        y = max(stones)
        stones.remove(y)
        
        stones.append(x-y)
    return stones[0]
%time lastStoneWeight(arr)<jupyter_output>CPU times: user 99 µs, sys: 5 µs, total: 104 µs
Wall time: 109 µs
{'fraction_non_alphanumeric': 0.09939593629873696, 'fraction_numerical': 0.036243822075782535, 'mean_word_length': 3.013215859030837, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '158470', 'license': 'MIT', 'n_tokens_mistral': 654, 'n_tokens_neox': 584, 'n_words': 250, 'path': 'Day_12_Last_Stone_Weight.ipynb', 'repo_name': 'bundickm/LeetCode-30-Day-Challenge', 'size': '1821'}