```
class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size();
if (n == 0) return 0;
int i = 0, j = min(n, citations.back())+1;
while (i < j) {
int mid = i + (j - i) / 2;
if (citations[n - mid] == mid || i == mid) return mid;
else if (citations[n - mid] > mid) i = mid; // Increase h: h can be mid.
else j = mid; // Decrease h.
}
return i;
}
};
```