Created
April 25, 2017 04:14
-
-
Save boris-42/a34a321605222808c56a34f5274d9418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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