-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh_index.h
More file actions
67 lines (58 loc) · 1.89 KB
/
Copy pathh_index.h
File metadata and controls
67 lines (58 loc) · 1.89 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
#ifndef CPP_ALGORITHM_H_INDEX_H
#define CPP_ALGORITHM_H_INDEX_H
#include <algorithm>
#include <ranges>
#include <vector>
namespace HIndex
{
/**
* \brief Given an array of citations of a researcher, write a function to compute the researcher's h-index.
* \param citations array of citations (each citation is a non-negative integer) of a researcher
* \return h-index
*/
int HIndex1(std::vector<int>& citations);
/**
* \brief Given an array of citations of a researcher, write a function to compute the researcher's h-index.
* \param citations array of citations (each citation is a non-negative integer) of a researcher
* \return h-index
*/
int HIndex2(std::vector<int>& citations);
}
// ----------------------------------------------------------------------------
inline int HIndex::HIndex1(std::vector<int>& citations)
{
// sort citations in descending order
std::ranges::sort(citations, std::greater<>{});
// std::sort(citations.begin(), citations.end(), std::greater<>());
int h_index = 0;
for (int i = 0; i < static_cast<int>(citations.size()); ++i)
{
// if current citation is greater than or equal to the number of papers
if (citations[i] >= i + 1)
{
h_index = i + 1;
}
else
{
break;
}
}
return h_index;
}
// ----------------------------------------------------------------------------
inline int HIndex::HIndex2(std::vector<int>& citations)
{
// sort citations in descending order
std::ranges::sort(citations, std::greater<>{});
// std::sort(citations.begin(), citations.end(), std::greater<>());
for (int i = 0; i < static_cast<int>(citations.size()); ++i)
{
// if current citation is less than the number of papers
if (citations[i] < i + 1)
{
return i;
}
}
return 0;
}
#endif