zoukankan      html  css  js  c++  java
  • 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."

    Example:

    Input: citations = [3,0,6,1,5]
    Output: 3 
    Explanation: [3,0,6,1,5] 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, her h-index is 3.

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

     
    AC code:
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            int len = citations.size();
            if (len == 0) return 0;
            vector<int> hash(len+1, 0);
            int maxIndex = 0;
            for (int i = 0; i < len; ++i) {
                if (len <= citations[i]) {
                    hash[len]++;
                } else {
                    hash[citations[i]]++;
                }
            }
            int paper = 0;
            for (int i = len; i >= 0; --i) {
                paper += hash[i];
                if (paper >= i)
                    return i;
            }
        }
    };
    

    Runtime: 4 ms, faster than 71.13% of C++ online submissions for H-Index.

    tips:

    maybe I can understand the intention of this problem.

    this is @yfcheng's explanation: 

    This type of problems always throw me off, but it just takes some getting used to. The idea behind it is some bucket sort mechanisms. First, you may ask why bucket sort. Well, the h-index is defined as the number of papers with reference greater than the number. So assume n is the total number of papers, if we have n+1 buckets, number from 0 to n, then for any paper with reference corresponding to the index of the bucket, we increment the count for that bucket. The only exception is that for any paper with larger number of reference than n, we put in the n-th bucket.

    Then we iterate from the back to the front of the buckets, whenever the total count exceeds the index of the bucket, meaning that we have the index number of papers that have reference greater than or equal to the index. Which will be our h-index result. The reason to scan from the end of the array is that we are looking for the greatest h-index. For example, given array [3,0,6,5,1], we have 6 buckets to contain how many papers have the corresponding index. Hope to image and explanation help.

    Buckets

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    C# LUA 闭包
    string与stringBuilder的效率与内存占用实测
    U3D assetbundle打包
    U3D assetbundle加载
    U3D临时文件GICache巨大
    Unity 协程Coroutine综合测试
    U3D协程Coroutine之WWW与Update()的并行测试
    U3D5.3.5f Monodevelop 仅支持到.NET 3.5
    安卓android杀不死进程,保护,双进程守护,驻留,Marsdaemon,保活
    html 音乐 QQ播放器 外链 代码 播放器 外链 代码
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9877299.html
Copyright © 2011-2022 走看看