Share my 4ms C++ code


  • 0
    Q
    static bool cmp(const int&a, const int &b)
    {
    	return a>b;
    }
    int hIndex(vector<int>& citations) {
    	int n = citations.size();
    	if (n == 0)
    		return 0;
    	if (n == 1)
    	{
    		if (citations[0] == 0)
    			return 0;
    		else
    			return 1;
    	}
    	sort(citations.begin(), citations.end(), cmp);
    	int h = 0;
    	for (int i = 0; i < n; i++)
    	{
    		if (citations[i] >= i + 1)
    			h = i + 1;
    	}
    	return h;
    }

Log in to reply
 

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