LeetCode 274. H-Index

1
2
3
4
5
6
7
8
9
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end());
int i = citations.size() - 1, h = 0;
while (i >= 0 && citations[i] > h) ++h, --i;
return h;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size(), c = 0;
vector<int> cnt(n + 1);
for (auto &x : citations)
++cnt[min(x, n)];
for (int i = n; i >= 0; i--)
if ((c += cnt[i]) >= i)
return i;
return 0;
}
};