clueless coding // TODO: be smarter

LeetCode H-Index: CPP Solution



TL;DR Code Solution


int hIndex(vector<int>& citations) {

    vector<int> res(citations.size() + 1, 0);

    for (int i = 0; i < citations.size(); i++) {
        if (citations[i] >= citations.size()) {
            res[citations.size()]++;
        }
        else {
            res[citations[i]]++;
        }
    }

    int total = 0;
    for (int i = res.size() - 1; i >= 0; i--) {
        total += res[i];
        if (total >= i) {
            return i;
        }
    }
    return 0;


}