Find the k-th largest element in the array, two different time complexity solutions priority_queue

I'm interested in two solutions that use priority_queue. Although both of them use priority_queue, I think they have different time complexity.

Solution 1:

int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int> pq(nums.begin(), nums.end()); //O(N)
        for (int i = 0; i < k - 1; i++) //O(k*log(k))
            pq.pop(); 
        return pq.top();
}

Time complexity: O (N) + O (k * log (k))

EDIT: sorry this should be O (N) + O (k * log (N)) thanks for pointing out!

Solution 2:

int findKthLargest(vector<int>& nums, int k) {
    priority_queue<int, vector<int>, greater<int>> p;
    int i = 0;
    while(p.size()<k) {

        p.push(nums[i++]);
    }

    for(; i<nums.size(); i++) {

        if(p.top()<nums[i]){
            p.pop();
            p.push(nums[i]);
        }

    }

    return p.top();
}

Time complexity: O (N * log (k))

So, in most cases, is the first solution much better than the second?

+4
source share
2 answers

O (n) + klog (n) O (n) + klog (k), n . k n, O (nlog (n)) .

k , O (nlog (k)) k n, O (nlog ()).

k , , k , . , :

k=1000
Code 1 time:0.123662
998906057
Code 2 time:0.03287
998906057
========
k=11000
Code 1 time:0.137448
988159929
Code 2 time:0.0872
988159929
========
k=21000
Code 1 time:0.152471
977547704
Code 2 time:0.131074
977547704
========
k=31000
Code 1 time:0.168929
966815132
Code 2 time:0.168899
966815132
========
k=41000
Code 1 time:0.185737
956136410
Code 2 time:0.205008
956136410
========
k=51000
Code 1 time:0.202973
945313516
Code 2 time:0.236578
945313516
========
k=61000
Code 1 time:0.216686
934315450
Code 2 time:0.27039
934315450
========
k=71000
Code 1 time:0.231253
923596252
Code 2 time:0.293189
923596252
========
k=81000
Code 1 time:0.246896
912964978
Code 2 time:0.321346
912964978
========
k=91000
Code 1 time:0.263312
902191629
Code 2 time:0.343613
902191629
========

, code1:

int findKthLargest2(vector<int>& nums, int k) {
    double st=clock();
    priority_queue<int, vector<int>, greater<int>> p(nums.begin(), nums.begin()+k);

    int i=k;
    for(; i<nums.size(); i++) {
        if(p.top()<nums[i]){
            p.pop();
            p.push(nums[i]);
        }

    }
    cerr<<"Code 2 time:"<<(clock()-st)/CLOCKS_PER_SEC<<endl;
    return p.top();
}
int findKthLargest1(vector<int>& nums, int k) {
        double st=clock();
        priority_queue<int> pq(nums.begin(), nums.end()); //O(N)
        for (int i = 0; i < k - 1; i++) //O(k*log(k))
            pq.pop(); 

        cerr<<"Code 1 time:"<<(clock()-st)/CLOCKS_PER_SEC<<endl;
        return pq.top();
}

int main() {   

 READ("in");
 vector<int>v;
 int n;
 cin>>n;
 repl(i,n)
 {
     int x;
     scanf("%d",&x);
     v.pb(x);
 }

 for(int k=1000;k<=100000;k+=10000)
 {
     cout<<"k="<<k<<endl;
    cout<<findKthLargest1(v,k)<<endl;
    cout<<findKthLargest2(v,k)<<endl;
    puts("========");
  }
}

1000000 0 10 ^ 9 , ++ rand().

+2

, O (N) + O (k * log (N)), - O (log (N))

int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int> pq(nums.begin(), nums.end()); //O(N)
        for (int i = 0; i < k - 1; i++) //O(k*log(N))
            pq.pop(); // this line is O(log(N))
        return pq.top();
}

- .

+1

Source: https://habr.com/ru/post/1598486/


All Articles