LeetCode-H Index

Algorithm record

LeetCode topic:

  You are given an integer array citations, where citations[i] is the number of times the researcher's ith paper has been cited. Calculates and returns the researcher's h-index.

  According to the definition of h-index on Wikipedia: h stands for "highly cited", and a researcher's h-index refers to his 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.

  If there are multiple possible values ​​of h, the h-index is the largest of them.


illustrate

1. The topic

输入:citations = [3,0,6,1,5]
输出:3 
解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
     由于研究者有 3 篇论文每篇至少被引用了 3 次,其余两篇论文每篇被引用不多于 3 次,所以她的 h 指数是 3。
复制代码

2. Analysis

  • Because at the beginning, we can assume that the number of citations of all papers is greater than zero, which is definitely true. After traversing the entire array, each time a citation larger than the current traversal element appears, it can be considered that the current number of papers is not the best, and it needs to be increased. .
  • However, if it is traversed directly, it may appear that small references are used to increase the result data, which will cause the data in the middle to be relatively large, so we need to sort first.
  • Traversing from large to small, if the current citation of the data we cycle through is greater than the number of our articles, it means that this article can be included in our high-citation sequence, and the number cannot be increased until the end.
class Solution {
    public int hIndex(int[] citations) {
        Arrays.sort(citations);
        int ret = 0, n = citations.length;
        for(int i = n - 1; i >= 0; i--) {
            if(citations[i] > ret) {
                ret++;
            }
        }
        return ret;
    }
}
复制代码

Summarize

Brain teasers.

Guess you like

Origin juejin.im/post/7101639290571980807