【发布时间】:2016-07-07 19:50:24
【问题描述】:
考虑以下代码,该代码从优先级队列中弹出前 2 个元素,将它们相加并将总和插入回优先级队列。
while (pq.size() > 1)
{
// Extract shortest two ropes from pq
int first = pq.top();
pq.pop();
int second = pq.top();
pq.pop();
// Connect the ropes: update result and
// insert the new rope to pq
res += first + second;
pq.push(first + second);
}
众所周知,将n个元素插入优先级队列是O(nlogn)操作。但是可以说优先级队列是作为数组实现的。 它不会变成 O(N*N) 操作。 或者上面 n 个元素的代码的复杂度是多少。
【问题讨论】:
标签: queue big-o complexity-theory priority-queue