274. H-Index
Description
Pitfall
Bucket sort 很难捉摸到,多写写吧
有个小窍门,如果 citation[i] >= citations.length, 可以直接放到末尾那个。
Solution
public int hIndex(int[] citations) {
final int n = citations.length;
final int[] buckets = new int[citations.length + 1];
for (int c : citations) {
if (c >= n) buckets[n]++;
else {
buckets[c]++;
}
}
int counts = 0;
for (int h = buckets.length - 1; h >= 0; h--) {
counts += buckets[h];
if (counts >= h) {
return h;
}
}
throw new IllegalStateException("You shouldn't be here");
}