【问题标题】:time complexity of priority_queue in C++C++中priority_queue的时间复杂度
【发布时间】:2021-04-28 18:59:11
【问题描述】:

我一直试图弄清楚priority_queue 的时间复杂度。 This 此处的帖子指出此容器的复杂性取决于底层容器。 我的问题是声明为priority_queue

    using mypair = std::pair<int, int>;
    std::priority_queue<std::pair<int, mypair>, std::vector<std::pair<int, mypair>>, std::greater<std::pair<int, mypair>>> heap;

pushpop 的时间复杂度是多少O(log(n))

【问题讨论】:

  • 计算时间取决于底层容器。时间复杂度(即 O() 表示法)没有。查找是O(N),插入和删除是O(N log N)。

标签: c++ time-complexity


【解决方案1】:

相对于树的高度,堆插入和提取具有最坏情况的复杂度,因此 O(log N),加上底层数据结构的复杂度。

如果是vectorpush_back 的摊销复杂度为 O(1),pop_back 的复杂度为 O(1)。

因此可以说,priority_queuevector 支持的pushpop 的总时间复杂度为 O(log N)。

【讨论】:

    猜你喜欢
    • 2017-05-05
    • 2021-08-01
    • 2022-06-10
    • 2018-02-21
    • 1970-01-01
    • 2016-02-13
    • 2012-08-14
    • 2013-03-14
    • 2021-02-08
    相关资源
    最近更新 更多