GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update 034.cpp · hfl15/algorithm@d8dd871

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • sword_offer/src
    • 034.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎sword_offer/src/034.cpp‎

Copy file name to clipboardExpand all lines: sword_offer/src/034.cpp
+127-92Lines changed: 127 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -6,111 +6,146 @@
66
77
[解析]
88
方法0: 直接排序然后返回前k个,最好的时间复杂度为 O(nlog(n))
9-
方法1: 快排的变种,时间复杂度 O(n),缺点:原址,需要把所有数都 load 到内存中
9+
方法1: 快排的变种,时间复杂度 O(n),缺点:修改输入;原址,需要把所有数都 load 到内存中
1010
方法2: 利用最大堆作为辅助,时间复杂度 O(n*lg(k)),适用于处理数据量很大的情况。
11+
方法3:使用红黑树,其查找可以控制在 lg(k),c++ 中 multiset 的实现就是基于红黑树的。
1112
*/
1213

1314
#include <iostream>
1415
#include <vector>
1516
#include <algorithm>
17+
#include <functional> // greater, less
18+
#include <set>
1619

1720
using namespace std;
1821

19-
class Solution{
20-
public:
21-
vector<int> GetLeastNumbers_Solution(vector<int> input, int k){
22-
vector<int> ans;
23-
if(k > input.size()){
24-
return ans;
25-
}
26-
27-
// GetLeastNumbersPartition(input, 0, input.size()-1, k);
28-
// for(int i=0; i<k; i++)
29-
// ans.push_back(input[i]);
30-
31-
// return ans;
32-
33-
return GetLeastNumbersHeap(input, k);
34-
}
35-
36-
// parition, average time complexity - O(n)
37-
// note: k should less than then size of input
38-
void GetLeastNumbersPartition(vector<int> &input, int left, int right, int k){
39-
int pos = partition(input, left, right);
40-
if(pos == k-1){
41-
return;
42-
}else if (pos < k-1){
43-
GetLeastNumbersPartition(input, pos+1, right, k);
44-
}else{
45-
GetLeastNumbersPartition(input, left, pos-1, k);
46-
}
47-
}
48-
49-
int partition(vector<int> &input, int left, int right){
50-
if(left > right)
51-
return -1;
52-
int pos = left-1;
53-
for(int i=left; i<right; i++){
54-
if(input[i] <= input[right]){
55-
swap(input[i], input[++pos]);
56-
}
57-
}
58-
59-
swap(input[right], input[++pos]);
60-
// input[left, pos] <= input[pos]
61-
// input[pos+1, right] > input[pos]
62-
return pos;
63-
}
64-
65-
// heap sort, time complexity - O(nlog(k))
66-
vector<int> GetLeastNumbersHeap(vector<int> &input, int k){
67-
68-
if(k > input.size() || input.empty())
69-
return vector<int>();
70-
71-
vector<int> ans(input.begin(), input.begin()+k); // max heap
72-
make_heap(ans.begin(), ans.end(), comp);
73-
74-
for(int i=k; i<input.size(); i++){
75-
if(input[i] < ans.front()){ // the current value less than the maximun of heap
76-
pop_heap(ans.begin(), ans.end(), comp);
77-
ans.pop_back();
22+
typedef multiset<int, greater<int> > iniSet;
23+
typedef multiset<int, greater<int> >::iterator setIter;
7824

79-
ans.push_back(input[i]);
80-
push_heap(ans.begin(), ans.end(), comp);
81-
}
82-
}
83-
84-
sort(ans.begin(), ans.end());
85-
86-
return ans;
87-
}
88-
89-
static bool comp(int a, int b){
90-
return a<b;
25+
class Solution {
26+
public:
27+
vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
28+
//return GetLeastNumbers_Partition(input, k);
29+
//return GetLeastNumbers_MaxHeap(input, k);
30+
return GetLeastNumbers_Multiset(input, k);
9131
}
9232

33+
vector<int> GetLeastNumbers_Partition(vector<int> &input, int k){
34+
if(input.size() < k || input.empty())
35+
return vector<int>();
36+
37+
int left = 0, right = input.size()-1;
38+
int index = Partition(input, left, right);
39+
while(index != k-1){
40+
if(index < k-1){
41+
left = index + 1;
42+
}else{
43+
right = index - 1;
44+
}
45+
index = Partition(input, left, right);
46+
}
47+
48+
vector<int> ans(k);
49+
for(int i=0; i<k; i++)
50+
ans[i] = input[i];
51+
return ans;
52+
}
53+
54+
int Partition(vector<int> &intput, int left, int right){
55+
if(intput.size() == 0 || left > right)
56+
return -1;
57+
if(left == right)
58+
return left;
59+
60+
int ileft = left+1;
61+
int iright = right;
62+
while(ileft < iright){
63+
while(ileft < iright && intput[ileft] < intput[left])
64+
ileft++;
65+
while(ileft < iright && intput[iright] >= intput[left])
66+
iright--;
67+
if(ileft < iright){
68+
swap(intput[ileft], intput[iright]);
69+
ileft++;
70+
iright--;
71+
}
72+
}
73+
swap(intput[left], intput[ileft-1]);
74+
return ileft-1;
75+
}
76+
77+
vector<int> GetLeastNumbers_MaxHeap(vector<int> &input, int k){ // 我本地测试通过,但提交到牛客网时报段错,通过率 0.00%。
78+
if(input.size() < k || input.empty())
79+
return vector<int>();
80+
81+
vector<int> ans(input.begin(), input.begin()+k);
82+
make_heap(ans.begin(), ans.end(), comp); // max heap
83+
for(int i=k; i<input.size(); i++){
84+
if(input[i] < ans.front()){
85+
pop_heap(ans.begin(), ans.end(), comp);
86+
ans[k-1] = input[i];
87+
push_heap(ans.begin(), ans.end(), comp);
88+
}
89+
}
90+
91+
return ans;
92+
}
93+
static bool comp(int a, int b){
94+
return a<b;
95+
}
96+
97+
vector<int> GetLeastNumbers_Multiset(vector<int> input, int k){
98+
if(input.size() < k || k < 1 || input.empty())
99+
return vector<int>();
100+
101+
iniSet ans;
102+
for(int i=0; i<input.size(); i++){
103+
if(ans.size() < k)
104+
ans.insert(input[i]);
105+
else{
106+
setIter itGreatest = ans.begin();
107+
if(input[i] < *itGreatest){
108+
ans.erase(itGreatest);
109+
ans.insert(input[i]);
110+
}
111+
}
112+
}
113+
114+
vector<int> ansVec(k);
115+
setIter it = ans.begin();
116+
int i=0;
117+
for( ; it != ans.end(); it++, i++){
118+
ansVec[i] = *it;
119+
}
120+
121+
return ansVec;
122+
}
93123
};
94124

125+
void printVector(vector<int>& vec){
126+
for(int i=0; i<vec.size(); i++)
127+
cout << vec[i] << " ";
128+
cout << endl;
129+
}
130+
95131
int main()
96132
{
97-
freopen("in.txt", "r", stdin);
98-
int k;
99-
100-
cin >> k;
101-
102-
vector<int> input;
103-
int cur;
104-
while(cin >> cur){
105-
input.push_back(cur);
106-
}
107-
108-
vector<int> ans = Solution().GetLeastNumbers_Solution(input, k);
109-
110-
for(int n : ans)
111-
cout << n << " ";
112-
cout << endl;
113-
114-
fclose(stdin);
115-
return 0;
116-
}
133+
// 8 4 5 1 6 2 7 3 8 4
134+
int n;
135+
cin >> n;
136+
vector<int> input(n);
137+
for(int i=0; i<n; i++)
138+
cin >> input[i];
139+
int k;
140+
cin >> k;
141+
cout << "The input array is: " << endl;
142+
printVector(input);
143+
cout << "k = " << k << endl;
144+
145+
Solution s = Solution();
146+
vector<int> ans = s.GetLeastNumbers_Solution(input, k);
147+
cout << "The answer is: " << endl;
148+
printVector(ans);
149+
150+
return 0;
151+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.