We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
do you knwo what is the difference between your code and:
for some reason having if A[0] >= k: inside while loop makes case 18 fail
def cookies(k, A):
# Write your code here
heapq.heapify(A)
count = 0
while len(A) >= 2:
if A[0] >= k:
return count
x = heapq.heappop(A)
y = heapq.heappop(A)
heapq.heappush(A, x+(2*y))
count += 1
return -1
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Jesse and Cookies
You are viewing a single comment's thread. Return to all comments →
Here is my solution. It uses python heapq library. It is fast
do you knwo what is the difference between your code and: for some reason having if A[0] >= k: inside while loop makes case 18 fail def cookies(k, A): # Write your code here heapq.heapify(A) count = 0 while len(A) >= 2: if A[0] >= k: return count x = heapq.heappop(A) y = heapq.heappop(A) heapq.heappush(A, x+(2*y)) count += 1 return -1