Skip to content

Instantly share code, notes, and snippets.

@boris-42
Created April 25, 2017 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boris-42/209a835ad8a3bfc8faf347fd2675b392 to your computer and use it in GitHub Desktop.
Save boris-42/209a835ad8a3bfc8faf347fd2675b392 to your computer and use it in GitHub Desktop.
class A(object):
def nchoc_bad(self, A, B):
myheap = []
for item in B:
heapq.heappush(myheap, -item)
#we have a heap containing all the numbers
result = 0
eaten = -heapq.heappop(myheap)
for i in xrange(A):
result = result + eaten
eaten = -int(math.floor(eaten/2))
eaten = - heapq.heappushpop(myheap, eaten)
return result % 1000000007
def nchoc_good(self, A, B):
heap = []
modulo = 10**9+7
for b in B:
heapq.heappush(heap, (-b, b))
result = 0
for i in xrange(A):
c = heapq.heappop(heap)
result = (result + c[1]) % modulo
heapq.heappush(heap, (-(c[1]/2), c[1]/2))
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment