Reputation: 4281
I'm interested in two solutions using priority_queue specifically. Although they both 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, it 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 the 1st solution is much better than the 2nd?
Upvotes: 5
Views: 1120
Reputation: 435
In the first case the complexity is O(n)+klog(n) not O(n)+klog(k) as there are n elements in the heap. In the worst case, k can be as large as n, so for unbounded data O(nlog(n)) is the correct worst case complexity.
In the second case, there is never more than k items in the priority queue, so the complexity is O(nlog(k)) and again for unbounded data k can be as large as n, so it is O(nlog(n)).
For smaller k, the second code will run faster, but as k becomes larger the first code becomes faster. I made some experiments, here are the results:
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
========
I modified the second code a little bit to make to similar to 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("========");
}
}
I used 1000000 random integers between 0 to 10^9 as dataset, generated by C++ rand() function.
Upvotes: 2
Reputation: 12442
well, no the first is O(N)+O(k*log(N)) because the pop is 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();
}
it's still better than the second in most cases.
Upvotes: 1