O(n) solution in JAVA, easy understanding


  • 0
    C
    public class Solution {
        public int hIndex(int[] citations) {
            int len = citations.length;
            if(0 == len) return 0;
            Arrays.sort(citations);
            for(int i = len-1; i >= 0; i--){
                if (citations[i] <= len-i) {
                    return citations[i] < len-i-1 ? len-i-1 : citations[i];
                }
            }
            return len;
        }
    }
    

Log in to reply
 

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