Sharing O(n) elegant Java solution


  • 0
    M
    public class Solution {
        public int hIndex(int[] citations) {
            
            int[] counts = new int[citations.length + 1];
            for (int c : citations) {
                counts[Math.min(citations.length, c)]++;
            }
            
            int sum = 0;
            for (int i=citations.length; i>=0; i--) {
                sum += counts[i];
                if (sum >= 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.