You are viewing a single comment's thread. Return to all comments →
import heapq def equalStacks(h1, h2, h3): heap = [(-sum(h), list(reversed(h))) for h in (h1, h2, h3)] heapq.heapify(heap) while any(s[0] != t[0] for s, t in zip(heap, heap[1:])): total, stack = heapq.heappop(heap) total += stack.pop() heapq.heappush(heap, (total, stack)) return -heap[0][0]
Seems like cookies are disabled on this browser, please enable them to open this website
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
Python, using heaps