J
Jorgen Grahn
for(..300K items..)
{
get(item);
pq.push(item);
if (pq.size() > 20)
pq.pop()
}
The priority queue always has the "highest"* value available for
immediate removal. Let's say there are 20 items in the priority
queue, stuff a new item in, and then you have 21, with the "highest"
one of those immediately removable. Since we only want the top 20,
just pull that extra one off and discard it.
Ah, of course! I was mentally placing the elements in the wrong
order, so that pop() would prematurely remove a candidate for the
top-20 list rather than an obvious non-candidate.
You're right, this is the obvious way to do it.
And perhaps I should be more used to dealing with priority queues.
I *do* know what it is, but I couldn't apply it correctly to this
problem.
/Jorgen