资讯专栏INFORMATION COLUMN

[Leetcode] H-Index H指数

xumenger / 843人阅读

摘要:排序法复杂度时间空间思路先将数组排序,我们就可以知道对于某个引用数,有多少文献的引用数大于这个数。代码排序得到当前的指数数组映射法复杂度时间空间思路也可以不对数组排序,我们额外使用一个大小为的数组。

H-Index I

Given an array of citations (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."

For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 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, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.

排序法 复杂度

时间 O(NlogN) 空间 O(1)

思路

先将数组排序,我们就可以知道对于某个引用数,有多少文献的引用数大于这个数。对于引用数citations[i],大于该引用数文献的数量是citations.length - i,而当前的H-Index则是Math.min(citations[i], citations.length - i),我们将这个当前的H指数和全局最大的H指数来比较,得到最大H指数。

代码
public class Solution {
    public int hIndex(int[] citations) {
        // 排序
        Arrays.sort(citations);
        int h = 0;
        for(int i = 0; i < citations.length; i++){
            // 得到当前的H指数
            int currH = Math.min(citations[i], citations.length - i);
            if(currH > h){
                h = currH;
            }
        }
        return h;
    }
}
数组映射法 复杂度

时间 O(N) 空间 O(N)

思路

也可以不对数组排序,我们额外使用一个大小为N+1的数组stats。stats[i]表示有多少文章被引用了i次,这里如果一篇文章引用大于N次,我们就将其当为N次,因为H指数不会超过文章的总数。为了构建这个数组,我们需要先将整个文献引用数组遍历一遍,对相应的格子加一。统计完后,我们从N向1开始遍历这个统计数组。如果遍历到某一个引用次数时,大于或等于该引用次数的文章数量,大于引用次数本身时,我们可以认为这是H指数。之所以不用再向下找,因为我们要取最大的H指数。那如何求大于或等于某个引用次数的文章数量呢?我们可以用一个变量,从高引用次的文章数累加下来。因为我们知道,如果有x篇文章的引用大于等于3次,那引用大于等于2次的文章数量一定是x加上引用次数等于2次的文章数量。

代码
public class Solution {
    public int hIndex(int[] citations) {
        int[] stats = new int[citations.length + 1];
        int n = citations.length;
        // 统计各个引用次数对应多少篇文章
        for(int i = 0; i < n; i++){
            stats[citations[i] <= n ? citations[i] : n] += 1;
        }
        int sum = 0;
        // 找出最大的H指数
        for(int i = n; i > 0; i--){
            // 引用大于等于i次的文章数量,等于引用大于等于i+1次的文章数量,加上引用等于i次的文章数量 
            sum += stats[i];
            // 如果引用大于等于i次的文章数量,大于引用次数i,说明是H指数
            if(sum >= i){
                return i;
            }
        }
        return 0;
    }
}
H-Index II

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

二分搜索法 复杂度

时间 O(logN) 空间 O(1)

思路

在升序的引用数数组中,假设数组长为N,下标为i,则N - i就是引用次数大于等于下标为i的文献所对应的引用次数的文章数。如果该位置的引用数小于文章数,则说明则是有效的H指数,如果一个数是H指数,那最大的H指数一定在它的后面(因为是升序的)。根据这点就可已进行二分搜索了。这里min = mid + 1的条件是citations[mid] < n - mid,确保退出循环时min肯定是指向一个有效的H指数。

代码
public class Solution {
    public int hIndex(int[] citations) {
        int n = citations.length;
        if(n == 0) return 0;
        int min = 0, max = citations.length - 1;
        while(min <= max){
            int mid = (min + max) / 2;
            // 如果该点是有效的H指数,则最大H指数一定在右边
            if(citations[mid] < n - mid){
                min = mid + 1;
            // 否则最大H指数在左边
            } else {
                max = mid - 1;
            }
        }
        // n - min是min点的H指数
        return n - min;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64578.html

相关文章

  • H-Index & II

    H-Index 题目链接:https://leetcode.com/problems... sort: public class Solution { public int hIndex(int[] citations) { if(citations == null || citations.length == 0) return 0; // sort ...

    mochixuan 评论0 收藏0
  • 274. H-Index

    摘要:题目解答满足这个的最大值不会超过数组的因为如果超过了,就不可能有这么多的数。所以就是把所有可能的个至少有个的记下来,然后找出最大的。因为是从后向前扫的,所以当前的就是满足条件的最大数。 题目:Given an array of citations (each citation is a non-negative integer) of a researcher, write a fun...

    xiaochao 评论0 收藏0
  • leetcode50 Pow(x, n)自定义实现指数运算

    摘要:题目要求此处为题目链接即用自己的代码实现指数运算。指数为负数即求其倒数。思路一二分法计算这题的思路我之前的一篇博客思路基本相同。所以在能转换为循环的情况下还是最好使用循环来解决。 题目要求 此处为题目链接即用自己的代码实现指数运算。指数运算一般有两种情况,即指数为整数和指数为负数的情况。指数为负数即求其倒数。 思路一:二分法计算 这题的思路我之前的一篇博客思路基本相同。有兴趣的可以直接...

    DoINsiSt 评论0 收藏0
  • 70道前端LeetCode题目集合及视频讲解(持续更新中...)

    前端LeetCode刷题 下面是已刷的题目的目录。GitHub:https://github.com/cunzaizhuy...每日打卡更新中,欢迎关注。 数组类 26 删除排序数组中的重复项 27 移除元素 35 搜索插入位置 66 加1 80 medium 删除排序数组中的重复项2 88 合并两个有序数组 167 两数之和II - 输入有序数组 118 杨辉三角 169 easy 求众数 1...

    mayaohua 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<