class Solution { public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { vector<int> res; if(input.size()<k||k<1) return res; if(input.size()==k) return input; multiset<int,greater<int>> max; multiset<int,greater<int>>::iterator it; for(int i=0;i<input.size();i++) { if(max.size()<k) max.insert(input[i]); else { it=max.begin(); if(input[i]<(*it)) { max.erase(*it); max.insert(input[i]); } } } it=max.begin(); while(it!=max.end()) { res.push_back(*it); it++; } return res; } };