题目

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."

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

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?

题意

设一个研究者共有n篇论文,如果其中有h篇,这h篇中每一篇都被引用过至少h次,而剩余的n-h篇中每一篇被引用的次数都不超过h,则称h为这个研究者的h指数。给定一个研究者n篇论文引用次数的升序数组,求该研究者h指数的最大值。

思路

0274. H-Index 相比,已经将数组按照升序排列,这样反而更简单。直接在 0274 的基础上使用二分查找进行改进即可。

代码实现

Java

class Solution {
    public int hIndex(int[] citations) {
        int left = 0, right = citations.length - 1;
        while (left < right) {
            int mid = (right - left) / 2 + left;
            if (citations[mid] >= citations.length - mid) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }

        return left == right && citations[left] >= citations.length - left ? citations.length - left : 0;
    }
}
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄