美文网首页
Leetcode in Java 275.H-Index II

Leetcode in Java 275.H-Index II

作者: 刺破羽毛 | 来源:发表于2020-01-10 22:34 被阅读0次

    英文题目:
    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."
    中文题目:
    给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列。编写一个方法,计算出研究者的 h 指数。
    h 指数的定义: “h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)至多有 h 篇论文分别被引用了至少 h 次。(其余的 N - h 篇论文每篇被引用次数不多于 h 次。)"


    此题与上题不同,上题给的数组是无序的,我们可以利用基于统计的排序将时间复杂度降为O(n),而本题citations保证了是有序的,我们可以利用二分查找将时间复杂度降为(log n)。此时应该很敏锐的想到应用二分查找法。

    public class Solution {
        public int hIndex(int[] citations) {
            int level = 0, n = citations.length, left = 0, right = n - 1;
            while(left <= right) {
                int mid = left + (right - left) / 2;
                if(citations[mid] >= n - mid) {  //一旦发现引用次数大于 n-mid,就在左半部查找可能更大的 level
                    level = n - mid;
                    right = mid - 1;
                }
                else left = mid + 1;  //说明左半部没有更大的 level,则在右半部查找
            }
            return level;
        }
    }
    

    相关文章

      网友评论

          本文标题:Leetcode in Java 275.H-Index II

          本文链接:https://www.haomeiwen.com/subject/nanmactx.html