Share my simple O(n) Java solution, O(1) space


  • 0
    Y

    The idea is to find the first element whose value is at least equal to the number of elements behind it(inclusive).
    '''
    public class Solution {
    public int hIndex(int[] citations) {
    Arrays.sort(citations);
    int res = 0;
    for(int i=0;i<citations.length;i++){
    if(citations[i]>0 && citations[i]>= citations.length-i){
    res = citations.length-i;
    break;
    }
    }
    return res;
    }
    }
    '''


Log in to reply
 

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