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. Jesse and Cookies
  2. Discussions

Jesse and Cookies

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 57 Discussions, By:

votes

Please Login in order to post a comment

  • thomas_owens282
    8 months ago+ 6 comments

    Python3 solution using the built in heapq library:

    import heapq
    
    def cookies(k, A):
        heapq.heapify(A)
        
        ops = 0
        while True:
            f = heapq.heappop(A)
                    
            if f >= k:
                return ops
            
            if len(A) == 0:
                return -1
            
            s = heapq.heappop(A)
            n = f + 2*s
            heapq.heappush(A, n)
            
            ops += 1
    
    15|
    Permalink
    View more Comments..
  • matvienkos1978
    9 months ago+ 3 comments

    3 days and about 10 attempts (include selfmade binary search for insert value to sorted array). I love it:). I solved this problem using two queues:

    In the first queues copy sorted list. In the second queue will write all calculated values (1cookie +2x 2cookie). This queue is automaticaly sorted because values increase . When extracting the value, select the minimum between two queues.

    Sorry of bad English, i'm just learning)

    https://github.com/smatvienko1978/MyFisrstRepository/blob/ced711c122e5f97b9313e73130b3729dfc1947a8/Program.cs

    13|
    Permalink
  • azmatmun
    7 months ago+ 1 comment

    in the first example they remove the cookie with sweetness '7' twice, once with 6 and once with 8. is this an error or am I misunderstanding the whole remove the cookies part?

    8|
    Permalink
  • ericrichardson18
    7 months ago+ 1 comment

    Something is up with the performance limits on this one. The number of test cases failing due to timeout ranges from 1 to 3 (literally running the exact same code). One test case in particular I ran in Groovy console and it took less than 0.5 seconds to get the correct answer, which seems like it should be well below the time limit.

    7|
    Permalink
  • joshualapo
    8 months ago+ 1 comment

    Relatively straightforward with the C++ standard library. I used multiset as the primary data structure.

    4|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature