Simple java O(n) solution


  • 0
    K
    public class Solution {
        public int hIndex(int[] citations) {
            if (citations == null || citations.length == 0) {
                return 0;
            }
            
            Arrays.sort(citations);
            int max = 0;
            for (int i = 0; i < citations.length; i++) {
                max = Math.max(max, Math.min(citations[i], citations.length - i));
            }
            
            return max;
        }
    }
    

  • 1
    Q
     Arrays.sort(citations);
    

    The complexity of this line is O(nlogn). Therefore, your solution is O(nlogn).


Log in to reply
 

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