https://leetcode.com/problems/h-index/

Source


    int hIndex(vector<int>& citations) {
        int max_cit = 0;
        for (const int& cit : citations) {
            if (cit > max_cit) max_cit = cit;
        }
        vi bins = create_bins(citations, max_cit+1);
        int h_idx = 0;
        int cnt = 0;
        FOR_DEC(cit, 0, bins.size()) {
            cnt += bins[cit];
            if (cit <= cnt) {
                h_idx = cit;
                break;
            }
        }
        return h_idx;
    }
    vi create_bins(const vi& data, int bin_size) {
        vi bins(bin_size, 0);
        for (const int& d : data) {
            if (d < bin_size) bins[d]++;
        }
        return bins;
    }

    int hIndex_sort(vector<int>& citations) {
        sort (begin(citations), end(citations), std::greater<>());
        int h = 0;
        int cnt = 1;
        for (const int& c: citations) {
            if (c >= cnt) h = cnt;
            else break;
            cnt++;
        }
        return h;
    }

GitHub

HIndex