c++ using hashmap


  • 0
    X
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            int len = citations.size();
            if(len == 0){
                return 0;
            }
            vector<int> hash(len + 1, 0);
            for(int i = 0; i < len; ++i){
                if(citations[i] >= len){
                    ++hash[len];
                }else{
                    ++hash[citations[i]];
                }
            }
            int num = 0;
            for(int i = len; i >= 0; --i){
                num += hash[i];
                if(num >= 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.