-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
41 lines (38 loc) · 951 Bytes
/
program.cpp
File metadata and controls
41 lines (38 loc) · 951 Bytes
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
#include "../include/pre.h"
int hIndex_sort(vector<int>& citations)
{
std::sort(citations.begin(), citations.end());
int max = citations.size();
//no parallel
for (auto ci : citations)
{
if (max > ci) max--;
else break;
}
return max;
}
// Runtime Error on Leetcode,
// But nothing suprised when I complie on my own computer
int hIndex(vector<int>& citations)
{
auto max_index = citations.size();
if (max_index == 0) return 0;
auto max = *std::max_element(citations.begin(), citations.end());
std::vector<int> count {max + 1, 0};
for (auto ci : citations) {
count[ci]++;
}
for (int i = 0; i != count.size(); i++)
{
if (count[i] == 0) continue;
if (max_index > i) max_index--;
else break;
}
return max_index;
}
int main()
{
vector<int> test = {1, 7, 9, 2,};//{7, 0, 6, 1, 5, 4};
cout << hIndex(test) << endl;
return 0;
}