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.

This problem is mistakenly placed into Queue subdomain, while it must be placed under 'Heap'.
I wasted some time trying to understand how it's possible to effectively use a queue (and not min-heap that immediately comes to mind).

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 →

This problem is mistakenly placed into Queue subdomain, while it must be placed under 'Heap'. I wasted some time trying to understand how it's possible to effectively use a queue (and not min-heap that immediately comes to mind).

hint : use priority_queue

priority queue is NOT a queue. It's a heap.

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.

It inherits from java.util.AbstractQueue, but it is NOT queue tho

In C++ as well, they have kept it under , may be while deciding the container, they might have given preference ot application over implementation.

It could be. But it is just so confusing at the first place

So did I!

You may sovel this by using two queues.

It is rightly placed under queue. I solved it using queue in O(n). Hint: Use two queue

It's impossible to solve this in O(n). You sorted the input - that's O(n log n).

You can sort in O(N) using Count Sort, the numbers in the array are < 10^6

C++ solution, passed all tests: