Java O(n) solution.


  • 3
    L
    public class Solution {
        public int hIndex(int[] citations) {
            int n = citations.length;
            int[] indexs = new int[n+1];
            for(int i = 0; i < n; i++) {
                if(citations[i]>=n) indexs[n]++; 
                else indexs[citations[i]]++;
            }
            for(int j = n; j >= 0; j--) {
                if(indexs[j] >= j) return j;
                if(j!=0) indexs[j-1] += indexs[j];
            }
            return 0;
        }
    }

  • -1
    S

    Hi,

    I came across a situation: given citations = [3, 1, 7, 8, 9]. What will be the answer be?

    According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."

    When h=3, the definition will be "A scientist has index 3 if 3 of his/her 5 papers have at least 3 citations each, and the other 2 papers have no more than 3 citations each." Obviously wrong....

    When h=4, the definition will be "A scientist has index 4 if 4 of his/her 5 papers have at least 4 citations each, and the other 1 papers have no more than 4 citations each." Obviously wrong....

    When h=2, the definition will be "A scientist has index 2 if 2 of his/her 5 papers have at least 2 citations each, and the other 3 papers have no more than 3 citations each." Obviously wrong....


  • 0
    L

    Hi shuyi@cvs, in reality, "A scientist has index h if h of his/her N papers have at least h citations each", this is just the case. The following definition "the other N − h papers have no more than h citations each." just makes it unclear.


  • 0
    S

    My previous understand(>= and <) is not correct, and the correct should be >= or <=


Log in to reply
 

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