class Solution:
def minimumDeviation(self, A: List[int]) -> int:
"""tc O(KNlgKN) K:lg(max(A)) sc O(N)
main idea: use max PQ. preprocess all odd number into even, keep halfing max even val until max number is odd
meanwhile keep a min_v to
"""
res = float('inf')
pq = [] # max PQ
for a in A:
heappush(pq,-a*2 if a%2==1 else -a)
minv = -max(pq)
while True: # if use pq[0]%2==0 need to compare pq[0]-minv with res in the end ; or we can use len(pq) to imply there is odd number to replace condition True since we only push back when a is even
a = -heappop(pq) # max val in array
res = min(res,a-minv)
if a %2 == 0:
minv = min(minv,a//2)
heappush(pq,-a//2)
else: break
return res