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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Heap
  4. Jesse and Cookies
  5. Discussions

Jesse and Cookies

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • mohammed_hasni11
    3 months ago+ 0 comments

    from heapq import heappush,heappop,heapify

    def cookies(k, A): heapify(A) iterations=0

    while len(A)>1 and A[0]<k and (A[1]<k or A[2]<k):
        a=heappop(A)
        b=heappop(A)
        heappush(A,a+2*b)
        iterations+=1
    
    if len(A)==1:
        if A[0] >= k:
            return iterations
        else:
            return -1
    else:
        if A[0] >= k:
            return iterations
        else:
            return iterations + 1
    

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    first_multiple_input = input().rstrip().split()
    
    n = int(first_multiple_input[0])
    
    k = int(first_multiple_input[1])
    
    A = list(map(int, input().rstrip().split()))
    
    result = cookies(k, A)
    
    fptr.write(str(result) + '\n')
    
    fptr.close()
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy