Easy to understand C++ implementation in 4ms


  • 0
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            int len=citations.size();
            if(len==0)  return 0;
            sort(citations.begin(), citations.end());
            /**
             * 3 0 6 1 5
             * 0 1 2 3 4
             * 5 4 3 2 1
             **/
            int result=0;
            for(int i=0; i<len; i++){
                int count=len-i;
                if(citations[i]>=count)  return count;
            }
            return 0;
        }
    };

Log in to reply
 

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