题目

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:

  1. Input: citations = [0,1,3,5,6]
  2. Output: 3
  3. Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
  4. received 0, 1, 3, 5, 6 citations respectively.
  5. Since the researcher has 3 papers with at least 3 citations each and the remaining
  6. 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 (M) 相比,已经将数组按照升序排列,这样反而更简单。直接在 0274 的基础上使用二分查找进行改进即可。


代码实现

Java

  1. class Solution {
  2. public int hIndex(int[] citations) {
  3. int left = 0, right = citations.length - 1;
  4. while (left < right) {
  5. int mid = (right - left) / 2 + left;
  6. if (citations[mid] >= citations.length - mid) {
  7. right = mid;
  8. } else {
  9. left = mid + 1;
  10. }
  11. }
  12. return left == right && citations[left] >= citations.length - left ? citations.length - left : 0;
  13. }
  14. }