Leetcode 0274: H-Index

Title description:

Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index.
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.”

Chinese description:

Given an array of the number of times a researcher’s paper has been cited (the number of times the paper has been cited is a non-negative integer). Write a method to calculate the researcher's h index.
Definition of h index: h stands for "high citations". The h index of a researcher refers to his (her) (out of N papers) with a total of h papers that have been cited at least h times. And each of the remaining N-h papers is cited no more than h times.
For example: a person's h index is 20, which means that in his published papers, each paper that has been cited at least 20 times has a total of 20 papers.

Example:

Input: citations = [3,0,6,1,5]
Output: 3
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had
received 3, 0, 6, 1, 5 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3.

Note:

If there are several possible values for h, the maximum one is taken as the h-index.

Time complexity: O O O (NNN)
Space complexity: O O O (NNN )
Counting sort:

  1. The general practice is to sort from largest to smallest. After sorting in descending order, there are i+1 papers in the sorted array [0, i] interval, and the number of citations for each article is at least citations [i] \mathrm{citations}[i ]citations[i]. 如果 c i t a t i o n s [ i ] \mathrm{citations}[i] citations[i] > \gt > i i i , then the papers 0 to i have at least i+1 citations. So we only need to find the largest i that satisfiescitations [i] \mathrm{citations}[i]citations[i] > \gt > i i i , then the index of h is i+1 (note that the array index starts from 0 after sorting. This practice Time complexity:OOO (N log N NlogNN l o g N )
  2. Note that in the process of calculating h index, the number of citations of papers may be very large, and this value is likely to exceed the total number of papers n. But if the number of citations of an article exceeds the total number of papers n, then reducing the number of citations to n will not change the value of the h index. So create a count array to count the frequency of occurrence of each citation number (0 ~ n) (that is, the number of papers with the current number of citations). When the number of citations exceeds the total number of papers n, it is also counted as n. Because it is the maximum value, the initial h index is t = n, and the current number of citation papers is cnt = count[n], if cnt >= t, it means that there are at least n papers that have been cited at least n times . Then t = n is the h index. If cnt <t, how can the index of h be the value of t minus 1: t–, and cnt += count[t]; means the number of papers with the number of citations to be added at this time. Loop until cnt >= t.
class Solution {
    
    
    public int hIndex(int[] citations) {
    
    
        int n = citations.length;
        int[] count = new int[n + 1];
        for (int c: citations){
    
    
            count[Math.min(n, c)]++;
        }
        int t = n;
        int cnt = count[n];
        while(cnt < t){
    
    
            t--;
            cnt += count[t];
        }
        return t;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43946031/article/details/114110056