274. H Index - cocoder39/coco39_LC GitHub Wiki

274. H-Index

h-index: h papers have at least h citations each. Thus h-index <= #paper. Figure this out, we have the idea of using bucket sort. O(n) time with O(n) memory

int hIndex(vector<int>& citations) {
        vector<int> dp(citations.size() + 1); //dp[i] is the number of papers that receieved i citations
        for (int i = 0; i < citations.size(); i++) {
            if (citations[i] > citations.size()) {
                dp[citations.size()]++;
            }
            else {
                dp[citations[i]]++;
            }
        }
        
        int cites = 0;
        for (int i = dp.size() - 1; i >= 0; i--) {
            cites += dp[i];
            if (cites >= i) {
                return i;
            }
        }
        return 0;
    }
⚠️ **GitHub.com Fallback** ⚠️