4ms cpp solution


  • 0
    Y
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            if (citations.empty())
            	return 0;
            sort(citations.begin(),citations.end());
            int size = citations.size();
            if (size <= citations[0])
                return size;
            for (int i = size - 1; i >= 0; i--) {
            	while (i - 1 >= 0 && citations[i] == citations[i - 1]) 
            		i--;
            	if (size - i >= citations[i])
            		return citations[i];
            	else if (i - 1 >= 0 && size - i >= citations[i - 1])
            	    return size - i;
            }
            return 0;
        }
    };

Log in to reply
 

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