-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30_KLeastNumbers.cpp
59 lines (48 loc) · 1.14 KB
/
30_KLeastNumbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <functional>
#include <set>
using namespace std;
vector<int> GetKLeastNums(vector<int> array, int k)
{
vector<int> result;
int len = array.size();
if (len == 0 || k <= 0)
return result;
multiset<int, greater<int> > s;
multiset<int, greater<int> >::iterator it;
for (int i = 0; i < len; i++)
{
if (s.size() < k)
{
s.insert(array[i]);
}
else
{
it = s.begin();
if (*it > array[i])
{
s.erase(it);
s.insert(array[i]);
}
}
}
for (it = s.begin(); it != s.end(); it++)
{
result.push_back(*it);
}
return result;
}
int main()
{
int n1[] = {4,5,1,6,2,7,3,8};
vector<int> nums1(n1, n1 + sizeof(n1)/sizeof(int));
vector<int> res1 = GetKLeastNums(nums1, 4);
vector<int> res2 = GetKLeastNums(nums1, 1);
for (int i = 0; i < res1.size(); i++)
cout << res1[i] << " ";
cout << endl;
for (int i = 0; i < res2.size(); i++)
cout << res2[i] << " ";
cout << endl;
}