Bucket Sort Solution without using extra space.


  • 0
    K
    public int hIndex(int[] citations) {
       int len = citations.length, temp = 0, index = 0;
         for (int i = 0; i < len; i++) {
    	if (citations[i] < 0) 
    	    continue;
    	index = citations[i] - 1;
    	citations[i] = 0;
    	while (index >= 0) {
    	     if (index >= len) 
    		 index = len - 1;
    	     if ((temp = citations[index]) > 0) {
    		  citations[index] = -1;
    		  index = temp -1;
                      continue;
    	     } 
    	     citations[index] = citations[index] - 1;
                 break;
    	}
          }
          for (int i = len - 1; i > -1; i--) {
    	if (0 - citations[i] >= (i+1)) 
    	return i+1;
    	if (i - 1 > 0) 
            citations[i - 1] = citations[i - 1] + citations[i];
           }
       return 0;
     }
    

Log in to reply
 

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