Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. vector<int> smallestK(vector<int>& a, int k)
  2. {
  3.     vector<int> maxHeap;
  4.    
  5.     for(int i=0;i<a.size();i++){
  6.         if(maxHeap.size()<k){
  7.             maxHeap.push_back(a[i]);
  8.             push_heap(maxHeap.begin(), maxHeap.end(), std::less<int>());
  9.         }
  10.         else if(a[i] < maxHeap[0]){
  11.             pop_heap(maxHeap.begin(), maxHeap.end(), std::less<int>());
  12.             maxHeap.pop_back();
  13.             maxHeap.push_back(a[i]);
  14.             push_heap(maxHeap.begin(), maxHeap.end(), std::less<int>());
  15.         }
  16.     }
  17.    
  18.     return maxHeap;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement