Easy java solution with comments , runs in 2ms


  • 2
    D

    public class Solution {
    public int hIndex(int[] citations) {

        //Base case : if array is empty, return 0
        if(citations==null || citations.length==0 )
            return 0;
        
        //if array has 1 element greater than 0, return 1    
        if(citations.length==1 && citations[0]>=1)
            return 1;
            
        //sort the array
        Arrays.sort(citations);
            
        //variable to keep track of length of array, hIndex
        int length=citations.length;
        int hIndex=0;
        
        //for loop to traverse the array
        for(int i=length-1;i>=0;i--)
        {
            //if value is present in array
            if(length-i==citations[i])
            {
                hIndex=citations[i];
            }
            else
            {
                //if value is not present in array
                if(i>0 &&citations[i-1]<=length-i && citations[i]>=length-i)
                    hIndex=length-i;
            }
            
            
        }
        
        //first element is greater than 0, and hash index is 0, return the length of the array as answer
        if(citations.length>1 && citations[0]>citations.length && hIndex==0)
            hIndex=citations.length;
        
        
        //return the answer
        return hIndex;
        
    }
    

    }


Log in to reply
 

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