Simplest Java Code with O(1) extra memory, no counter array


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

Log in to reply
 

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