Share my simple short O(n) Java solution


  • 0
    M
        public int hIndex(int[] citations) {
            if (citations == null || citations.length == 0) return 0;
    		int n = citations.length;
    		int[] c = new int[n + 1];
    		for (int i : citations) {
    			if (i > n) i = n;
    			++c[i];
    		}
    		if (c[n] >= n) return n;
    		for (int i = n - 1; i > 0; --i) {
    			c[i] += c[i + 1];
    			if (c[i] >= i) return i;
    		}
    		return 0;
        }
    

Log in to reply
 

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