考虑下面的代码,它从优先级队列中弹出前两个元素,将它们相加,并将总和插入到优先级队列中。
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个元素的上述代码的复杂度是多少。
https://stackoverflow.com/questions/38254134
复制相似问题