-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path347.cpp
More file actions
43 lines (34 loc) · 1.63 KB
/
347.cpp
File metadata and controls
43 lines (34 loc) · 1.63 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
#include <vector>
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main()
{
vector<int> nums = {1, 1, 3, 2, 2 ,2 ,2}; // size = 7
int k = 1;
vector <int> output;
// saving ocurrences
unordered_map <int, int>hashmap;
for(const int& it : nums){
hashmap[it]++;
}
// bucket sort algorithm
vector<vector<int>> bucketSort (nums.size()+1); // 7 + 1 -> remember that we are not using the 0 slot, since doesn't make sence to use it
// hence we need one extra space
// filling the bucketSort vector
for(const auto& pair : hashmap){
bucketSort.at(pair.second).push_back(pair.first); // using number of occurences as index for the bucketSort vector -- remember that our approach is using this vector's index as number of ocrrences. since the nums lenght is 7, the max index this vector can have is 7. this is the worst case scenario (imagine that the same number repites 7 times, that would be 7 numbers in the same index 7)
}
// sorting the buckedSort vector for getting the top k repeated elements
for (int i = bucketSort.size()-1; i > 0 ; i--) // the for loop would go from (i > 0 && i = 8) -> (1 - 7), i= 8 to get the slot where 7 ocurrences happened, remember that we dont care about the 0 slot
{
for (int j = 0; j < bucketSort.at(i).size(); j++)
{
if(k == 0) return output;
output.push_back(bucketSort.at(i).at(j)); // getting the numbers from each slot (all repeated numbers (in a slot) since remember that they are occurences)
k--;
}
}
return 0;
}