c++ vector O(n) time solution


  • 0
    D
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            vector<int> v(citations.size() + 1, 0);
            for(int i = 0; i < citations.size(); i++) {
                if(citations[i] >= citations.size()) v[citations.size()]++;
                else v[citations[i]]++;
            }
            int hidx = 0;
            
            for(int i = citations.size(); i >= 0; i--) {
                hidx += v[i];
                if(hidx >= i) return i;
            }
            return 0;
        }
    };
    

Log in to reply
 

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