public class Solution {
public int hIndex(int[] citations) {
// sorting the citations in ascending order
Arrays.sort(citations);
// finding h-index by linear search
int i = 0;
while (i < citations.length && citations[citations.length - 1 - i] > i) {
i++;
}
return i; // after the while loop, i = i' + 1
}
}
public class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int[] papers = new int[n + 1];
// counting papers for each citation number
for (int c: citations)
papers[Math.min(n, c)]++;
// finding the h-index
int k = n;
for (int s = papers[n]; k > s; s += papers[k])
k--;
return k;
}
}
public class Solution {
public int hIndex(int[] citations) {
if (citations == null || citations.length < 1) return 0;
Arrays.sort(citations);
int res = 0;
int len = citations.length;
for (int i = len - 1; i >= 0; i--) {
if (citations[i] > res) {
res++;
}
}
return res;
}
}