简体   繁体   English

该算法在 Big(O) 中的时间复杂度

[英]Time complexity of this algorithm in Big(O)

I came up with the following algorithm to calculate the time complexity to find the second most occuring character in a string.我想出了以下算法来计算时间复杂度,以找到字符串中出现次数第二多的字符。 This algo is divided into two parts.该算法分为两部分。 The first part where characters are inserted into a map in O(n).在 O(n) 中将字符插入 map 的第一部分。 I am having difficulty with the second part.我对第二部分有困难。 Iterating over the map is O(n) push and pop is O(log(n)).遍历 map 是 O(n) 推入和弹出是 O(log(n))。 what would be the BigO complexity of the second part?第二部分的 BigO 复杂性是多少? finally what would the overall complexity be?最后,整体复杂性是多少? Any help understanding this would be great?任何帮助理解这一点会很棒吗?

void findKthHighestChar(int k,std::string str)
{
    std::unordered_map<char, int> map;
    
    //Step 1: O(n)
    for (int i = 0; i < str.size(); i++)
    {
        map[str[i]] = map[str[i]] + 1;
    }

    //Step2: O(n*log())
    //Iterate through the map
    using mypair = std::pair<int, char>;
    std::priority_queue<mypair, std::vector<mypair>, std::greater<mypair>> pq;
    for (auto it = map.begin(); it != map.end(); it++) //This is O(n) .
    {
        pq.push(mypair(it->second, it->first)); //push is O(log(n))

        if (pq.size() > k) {
            pq.pop();                           //pop() is O(log(n))
        }
    }
    std::cout << k << " highest is " << pq.top().second;
}

You have 2 input variables, k and n (with k < n ).您有 2 个输入变量, kn (与k < n )。
And one hidden: alphabet size A还有一个隐藏:字母大小A

  • Step1 has average-case complexity of O(n) . Step1 的平均情况复杂度为O(n)

  • Step2: O(std::min(A, n)*log(k)) .第2步: O(std::min(A, n)*log(k))

    Iterating the map is O(std::min(A, n))迭代 map 为O(std::min(A, n))

    Queue size is bound to k , so its operation are in O(log(k))队列大小绑定到k ,所以它的操作在 O(log(k))

Whole algorithm is so O(n) + O(std::min(A, n)*log(k)) If we simplify and get rid of some variables to keep only n :整个算法是如此O(n) + O(std::min(A, n)*log(k))如果我们简化并去掉一些变量以仅保留n

  • ( k -> n , A -> n ): O(n) + O(n*log(n)) so O(n*log(n)) . ( k -> n , A -> n ): O(n) + O(n*log(n))所以O(n*log(n))
  • ( k -> n , std::min(A, n) -> A ) : O(n) + O(log(n)) so O(n) . ( k -> n , std::min(A, n) -> A ) : O(n) + O(log(n))所以O(n)

Does it have to be this algorithm?一定是这个算法吗?

You can use an array (of the size of your alphabet) to hold the frequencies.您可以使用一个数组(字母大小)来保存频率。

You can populate it in O(n), (one pass through your string).您可以在 O(n) 中填充它,(通过您的字符串)。 Then you can find the largest, or second largest, frequency in one pass.然后,您可以一次找到最大或第二大频率。 Still O(n).仍然是 O(n)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM