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 →

  • coleslawstl
    5 years ago+ 1 comment

    solution for python 3 using heapq

    from heapq import heappop,heappush,heapify
    
    firstLine = [int(x) for x in input().split()]
    cookies = [int(x) for x in input().split()]
    
    cookieCount = int(firstLine[0])
    minSweetness = int(firstLine[1])
    
    heapify(cookies)
    
    fC = 0
    try:
        while cookies[0] < minSweetness:
            fC+=1
            c1 = heappop(cookies)
            c2 = heappop(cookies)
            newCookie=(1*c1)+(2*c2)
            heappush(cookies,newCookie)
        print(fC)
    except:
        print("-1")
    
    15|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature