Quick and clean answer in C++ with explaination


  • 0
    P
    int hIndex(vector<int>& citations) {
    	//first descend sort, then index of current element stand for the num of bigger than current element.
    	sort(citations.begin(),citations.end(),greater<int>());
    
    	//we are going to find the maximum one that element is no less than index .
    	// e.g.
    	// 6 5 3 1 0
    	// 1 2 3 4 5
    	for(int i=citations.size()-1;i>-1;i--)
    	{
    		if( citations[i] >= i+1 )
    			return i+1;
    	}
    	return 0;
    }

Log in to reply
 

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