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.

You can solve this without a heap, only use pure Queue. You need to sort the input first, and then use two simple Queue. An init queue and an other to store the new made cookies.
Every time a cookie is made, the time complexity will be constant time.
The final time complexity will be O(nlogn + number of operations).

## Jesse and Cookies

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

oh. then, use 2 queues. but, sort it before u push the inputs to the first queue.

That makes no sense sir. This question requires a min heap priority queue which in reality is not a f-i-f-o queue.

You can solve this without a heap, only use pure Queue. You need to sort the input first, and then use two simple Queue. An init queue and an other to store the new made cookies. Every time a cookie is made, the time complexity will be constant time. The final time complexity will be O(nlogn + number of operations).

a heap can be used to implement a priority que and thus they are synonymos.

A[0] remains empty A[1] is the heap head (max or min your choice) and for A[k] the left node is at A[2k] and the right at A[2k + 1]

here is my solution using a PriorityQueue

My solution is exactly like this, but 2 cases are failing because of timeout. How did you resolve it?

Check your while should contain both conditions while (queue.size() > 1 && queue.peek() < K) because while (queue.peek() < K) won't suffice.

you might be lucky...my two test cases are also failing due to TLE.