Ac solution code


  • 0

    The basic idea is from bucket sort: count the times of each number in citations which is less than citations size len, then sum the times for each number from len..0, as the following:

    public int hIndex(int[] citations) {
    	int len = citations.length;
    	int[] count = new int[len + 1];
    	
    	for (int i : citations) {
    		if (i > len)
    			count[len]++;
    		else
    			count[i]++;
    	}
    	
    	int total = 0;
    	for (int i = len; i >= 0; i--) {
    		total += count[i];
    		if (total >= i)
    			return i;
    	}		
    	return 0;
    }

Log in to reply
 

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