c++
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end());
int h = 1, n = citations.size();
while (h <= citations.size() && citations[--n] >= h) h++;
return h - 1;
}
};
js
var hIndex = function(citations) {
citations.sort((a, b) => a - b);
var h = 1, n = citations.length;
while (h <= n && citations[n - h] >= h) h++;
return h - 1;
};