Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First-heapq-in place solution in Clear category for The Final Stone by vtflnk
import heapq
def final_stone(stones: list[int]) -> int:
for i, stone in enumerate(stones):
stones[i] = -stone
heapq.heapify(stones)
len_ = len(stones)
while True:
if len_ == 0:
return 0
if len_ == 1:
return -stones[0]
s1 = heapq.heappop(stones)
s2 = heapq.heappop(stones)
heapq.heappush(stones, -abs(s1-s2))
len_ -= 1
print('Example:')
print(final_stone([1,2,3]))
assert final_stone([3, 5, 1, 1, 9]) == 1
assert final_stone([1, 2, 3]) == 0
assert final_stone([1, 2, 3, 4]) == 0
assert final_stone([1, 2, 3, 4, 5]) == 1
assert final_stone([1, 1, 1, 1]) == 0
assert final_stone([1, 1, 1]) == 1
assert final_stone([1, 10, 1]) == 8
assert final_stone([1, 10, 1, 8]) == 0
assert final_stone([]) == 0
assert final_stone([1]) == 1
assert final_stone([10, 20, 30, 50, 100, 10, 20, 10]) == 10
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 13, 2022
Comments: