My Java solution using sort


  • 0
    A
    public class Solution {
        public int hIndex(int[] citations) {
            Arrays.sort(citations);
            int ans = 0;
            int l = citations.length;
            
            for(int i=0;i<l;i++){
                int c = citations[i];
                if(ans == c){
                    continue;
                }
                //l-i gives us the citations from i to l-1,citations are sorted,  so if l-i >= ans then
                // ans is one possible contender for answer
                if(c > ans && l-i >= ans){
                    ans = Math.min(c,l-i);
                }
            }
            
            return ans;
        }
    }
    

Log in to reply
 

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