-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH-Index.cpp
More file actions
36 lines (36 loc) · 790 Bytes
/
H-Index.cpp
File metadata and controls
36 lines (36 loc) · 790 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
//H-Index,https://leetcode.com/problems/h-index/
//直接先排序
int hIndex(vector<int>& citations)
{
if (citations.empty())
return 0;
sort(citations.begin(), citations.end());
int n = citations.size();
int i = 0;
while (i < n && citations[i] < (n - i))
i++;
return n - i;
}
//H-Index,https://leetcode.com/problems/h-index/
//hashtable,时间复杂度为O(N)
int hIndex_v2(vector<int>& citations)
{
if (citations.empty())
return 0;
int n = citations.size();
vector<int> hash(n + 1, 0);
for (int i = 0; i < n; ++i)
{
if (citations[i] >= n)
hash[n]++;
else
hash[citations[i]]++;
}
int paper = 0;
for (int i = n; i >= 0; --i)
{
paper += hash[i];
if (paper >= i)
return i;
}
}