-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40.getLeastNumbersK.cpp
More file actions
executable file
·99 lines (89 loc) · 2.27 KB
/
Copy path40.getLeastNumbersK.cpp
File metadata and controls
executable file
·99 lines (89 loc) · 2.27 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
@filename getLeastNumbersK.cpp
@author caonan
@date 2022-04-20 06:32:10
@reference 剑指offer
@url https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/
@brief 输入整数数组 arr ,找出其中最小的 k
个数。例如,输入4、5、1、6、2、7、3、8这8个数字,则最小的4个数字是1、2、3、4。
*/
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution
{
public:
//时间复杂度 nlogk做法
vector<int> getLeastNumbers(vector<int>& arr, int k)
{
vector<int> ans;
if (!k) return ans;
priority_queue<int> q; // c++的实现默认是大根堆
for (int i = 0; i < k && i < arr.size(); i++) {
q.push(arr[i]);
}
for (int i = k; i < arr.size(); i++) {
if (q.top() > arr[i]) {
q.push(arr[i]);
q.pop();
}
}
while (!q.empty()) {
ans.push_back(q.top());
q.pop();
}
return ans;
}
};
class Solution1 : public Solution
{
public:
// n+klogn做法,自己实现堆todo:
vector<int> getLeastNumbers(vector<int>& arr, int k) {
}
};
class Solution2 : public Solution
{
public:
// n+klogn做法,用优先队列的小根堆
vector<int> getLeastNumbers(vector<int>& arr, int k)
{
vector<int> ans;
if (!k) return ans;
auto cmp = [](int left, int right) { return left > right; }; //定义小根堆
std::priority_queue<int, std::vector<int>, decltype(cmp)> q(cmp);
for (auto n : arr) {
q.push(n);
}
while (!q.empty() && k--) {
ans.push_back(q.top());
q.pop();
}
return ans;
}
};
void uinttest(vector<int> arr, int k)
{
Solution2 s;
auto ret = s.getLeastNumbers(arr, k);
for (auto v : ret) {
cout << v << " ";
}
cout << endl;
}
int main()
{
uinttest(vector<int>{1, 3, 6, 2, 88, 9}, 4);
uinttest(vector<int>{1, 3, 6, 2, 88, 9}, 0);
uinttest(vector<int>{1, 3}, 3);
uinttest(vector<int>{3, 2, 1}, 2);
return 0;
}