O(n) 2ms simple solution in Java


  • -3
    R
    public class Solution {
    public int hIndex(int[] cit) {
        Arrays.sort(cit);
        if (cit == null || cit.length == 0)
            return 0; 
        int n = cit.length;
        if (cit[0] == 0 && cit[n-1] == 0) return 0;
        int h = 0; int count = 0;
        for (int i = n-1;i>=0;i--) {
            h++;
            if (cit[i] >= h) {
                if (i-1 >= 0 && cit[i-1] <= h)
                  return h;
            }
        }
        return h;
    }
    

    }


  • 0
    J

    Arrays.sort(cit) is O(NlogN).


Log in to reply
 

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