```
public class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
if ( n == 0) return 0;
if (citations[n-1] == 0) return 0;
int res = 0;
for (int i = citations.length - 1; i >= 0; i--) {
if (citations[i] >= res) {
res++;
if (res > citations[i]) res--;
}
else break;
}
return res;
}
}
```