DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

Leetcode - 274. H-Index

/**
 * @param {number[]} citations
 * @return {number}
 */
var hIndex = function(citations) {

   for(let h = citations.length ; h>0;h-- ){
        let count = 0 
        for(let i= 0 ; i <citations.length;i++){

            if(citations[i]-h >=0){
                count++;
            }

        }
        if(count >= h){
            return h
        }
   }
   return 0
};
Enter fullscreen mode Exit fullscreen mode

With Sorting
Image description

var hIndex = function(citations) {
    citations = citations.sort((a,b)=>b-a);
    for(i=citations.length; i>0; i--)
        if(citations[i-1]>=i)
            return i
    return 0    
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)