You are viewing a single comment's thread. Return to all comments →
Python solution with heapq
import heapq def cookies(k, A): heapq.heapify(A) minVal = heapq.heappop(A) if minVal >= k: return 0 heapq.heappush(A, minVal) count = 0 while(len(A) >= 2): min1 = heapq.heappop(A) min2 = heapq.heappop(A) heapq.heappush(A, min1 + min2 * 2) count += 1 minNew = heapq.heappop(A) if minNew >= k: return count heapq.heappush(A, minNew) return -1
Seems like cookies are disabled on this browser, please enable them to open this website
Jesse and Cookies
You are viewing a single comment's thread. Return to all comments →
Python solution with heapq