当前位置:   article > 正文

Leetcode275 H指数II

Leetcode275 H指数II

题目描述

给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数,citations 已经按照 升序排列 。计算并返回该研究者的 h 指数。

h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (n 篇论文中)至少 有 h 篇论文分别被引用了至少 h 次。

请你设计并实现对数时间复杂度的算法解决此问题。

解题思路

基于前一题的解题思路Leetcode274 H指数,可以把查找H指数的过程改成二分法,因为此题中H指数数组是已经排序好的,这样就可以实现log(N)时间复杂度。

代码实现

public static int hIndex(int[] citations) {
        int s = 0, e = citations.length - 1, r = citations[0] > 0 ? 1 : 0;
        while (s < e) {
            int mid = (s + e) / 2;
            if (citations[mid] >= citations.length - mid) {
                r = citations.length - mid;
                e = mid;
            } else {
                s = mid + 1;
            }
        }
        // 防止出现特殊情况没有扫描到
        if (citations[s] >= citations.length - s && (citations.length - s) > r) {
            r = citations.length - s;
        }
        return r;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号