274. H-Index

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.

Solution:Bucket

思路:建一个bucket数组len=paper总数量,里面存的是citations数 对应的 paper数量,当citations数 > paper总数量,都放在length位置就ok,因为h-index最多只能paper总数量。
再从后遍历bucket并累积(paper数量累积),输出第一个大于index(citations数)的index值
Time Complexity: O(N) Space Complexity: O(N)

Solution Code:

class Solution {
    public int hIndex(int[] citations) {
        int len = citations.length;
        if(len == 0) return 0;
        
        // build a bucket where the index is the num of citations
        // and the value is num of papers
        int bucket[] = new int[len + 1];
        for(int i = 0; i < len; i++) {
            if(citations[i] > len) {
                bucket[len]++;
            }
            else bucket[citations[i]]++;
        }
        
        //get result from bucket
        int paper_num = 0;
        for(int i = len; i >= 0; i--) {
            paper_num += bucket[i];
            if(paper_num >= i) return i;
        }
        
        return 0;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问题 Given an array of citations (each citation is a non-ne...
    RobotBerry阅读 3,935评论 0 0
  • 第五话 「阴云密布」 文:YuShi 1 一放学,小椿就拉着我和阿渡往医院跑。 “小薰!我们来看你啦!”小椿一边大...
    YuShi_阅读 4,844评论 10 9
  • 冬夜,一片枯叶,思念天空的微笑,明亮的借口,是对光阴要走的挽留。黑暗中,才开始关注,身体,早已失去水分的身体。 冬...
    宇斯阅读 1,450评论 0 3
  • 在心理咨询中,倾听是必备技能,PET也如此。 前半年忙于工作忽视女儿,诸多行为的退行,连摔跤也会大闹的情绪经常发生...
    文刀祐阅读 3,229评论 0 1
  • 2017年7月21日是咱们组第三次作业,我因为在孩子学校参加一个准备会,所以没能参与大范围的点评战友作业,小组的作...
    一汪青水阅读 1,499评论 2 5

友情链接更多精彩内容