Simple solution using no extra space C++


  • 0
    P
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            // Sort the array first
    	    sort(citations.begin(), citations.end());
    
    	    // [0 1 3 5 6]
    	    int hindex = 0, diff;
    	    for(int i = 0; i < citations.size(); i++)
    	    {
    		    diff = citations.size() - i;
    		    if(citations[i] >= diff && hindex < diff)
    			    hindex = diff;
        	}
    
    	    return hindex;
        }
    };

Log in to reply
 

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