4ms c++ without sort, beats 100%


  • 3
    X
    int hIndex(vector<int>& citations) {
            if (citations.empty())
                return 0;
            vector<int> count(citations.size()+1, 0); // record 0-size
            for (int i = 0; i < citations.size(); ++i)
            {
                count[citations[i] < count.size() ? citations[i] : count.size() - 1]++;
            }
            int sum = 0;
            for (int j = count.size() - 1; j >= 0; --j)
            {
                sum += count[j];
                if (j <= sum)
                    return j;
            }
            return 0;
        }

Log in to reply
 

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