[
heap
simulation
]
BinarySearch 0416 Cell Fusion
Problem statement
https://binarysearch.com/problems/Cell-Fusion/
Solution
Just simulate process, using heap.
Complexity
It is O(n log n)
for time and O(n)
for space.
Code
class Solution:
def solve(self, cells):
h = [-x for x in cells]
heapify(h)
while h:
if len(h) == 1: return -h[0]
x = heappop(h)
y = heappop(h)
if x != y:
heappush(h, -((-x + -y)//3))
return -1