-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
347.cpp
70 lines (65 loc) · 1.76 KB
/
347.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
60
61
62
63
64
65
66
67
68
69
70
__________________________________________________________________________________________________
sample 12 ms submission
static int fast = []() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 0;
}();
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> m;
vector<int> res;
int i = 0;
for(auto num: nums)
i = max(i,++m[num]);
while(res.size() != k){
for(auto p : m)
if(p.second == i)
res.push_back(p.first);
--i;
}
return res;
}
};
__________________________________________________________________________________________________
sample 10840 kb submission
class NumCount {
public:
int val;
int count;
NumCount(int i) {
val = i;
count = 1;
}
bool operator<(NumCount &i) {
return i.count < count;
}
};
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
vector<NumCount> numbers;
for(int i = 0; i < nums.size(); i++) {
int j = 0;
for(; j < numbers.size(); j++) {
if(nums[i] == numbers[j].val) {
numbers[j].count++;
break;
}
}
if(j >= numbers.size()) {
NumCount num_count(nums[i]);
numbers.push_back(num_count);
}
}
sort(numbers.begin(), numbers.end());
vector<int> ret_val;
for(int i = 0; i < k; i++) {
ret_val.push_back(numbers[i].val);
}
return ret_val;
}
};
__________________________________________________________________________________________________