解法1
从小到大排序。
计算可以接入的最大正方形。
class Solution {
public int hIndex(int[] citations) {
Arrays.sort(citations);
int h = 0, i = citations.length - 1;
while (i > -1 && citations[i] > h) {
h++;
i--;
}
return h;
}
}
解法2
H指数最大为已发表文章数。
分桶计数。
从右向左遍历,若文章数为i,且当前共有tot篇文章引用数大于等于i,则i为h指数
class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int[] counter = new int[n + 1];
for (int i = 0; i < n; i++) {
if (citations[i] > n) {
counter[n]++;
} else {
counter[citations[i]]++;
}
}
int tot = 0;
for (int i = n; i > -1; i--) {
tot += counter[i];
if (tot >= i) {
return i;
}
}
return 0;
}
}