C++ concise solution(20 lines) with O(n) complexity.


  • 0
    H
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            const int n = citations.size();
            vector<int> counter(n + 1, 0);
            for (int num : citations) {
                num = min(num, n);
                ++counter[num];
            }
            int acc = 0;
            int h = 0;
            for (int i = n; i >= 1; --i) {
                counter[i] += acc;
                acc = counter[i];
                h = max(h, min(i, counter[i]));
            }
            return h;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.