275. H-Index II

Given an array of citations sorted in ascending order (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.”

Example:

Input: citations = [0,1,3,5,6]
Output: 3 
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had received 0, 1, 3, 5, 6 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.

Follow up:

This is a follow up problem to H-Index, where citations is now guaranteed to be sorted in ascending order.
Could you solve it in logarithmic time complexity?

难度:medium

题目:给定一个研究人员的引用数组(每个引用都是一个非负整数),编写一个函数来计算研究人员的h-index。
根据维基百科关于h-index的定义,一个科学家的h-index为h,即其N篇文章中有h篇的引用不低于h, 并且其它文章引用数不超过h.

思路:弄清题义即可。

Runtime: 3 ms, faster than 100.00% of Java online submissions for H-Index II.
Memory Usage: 43 MB, less than 6.00% of Java online submissions for H-Index II.

class Solution {
    public int hIndex(int[] citations) {
        if (null == citations || citations.length < 1) {
            return 0;
        }

        int n = citations.length;
        int hIndex = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (citations[i] >= n - i) {
                hIndex++;
            } else {
                break;
            }
        }
        
        return hIndex;
    }
}
    原文作者:linm
    原文地址: https://segmentfault.com/a/1190000018346806
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞