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數(shù)組len=paper總數(shù)量,里面存的是citations數(shù) 對應(yīng)的 paper數(shù)量,當(dāng)citations數(shù) > paper總數(shù)量,都放在length位置就ok,因為h-index最多只能paper總數(shù)量。
再從后遍歷bucket并累積(paper數(shù)量累積),輸出第一個大于index(citations數(shù))的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;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問題 Given an array of citations (each citation is a non-ne...
    RobotBerry閱讀 726評論 0 0
  • 第五話 「陰云密布」 文:YuShi 1 一放學(xué),小椿就拉著我和阿渡往醫(yī)院跑。 “小薰!我們來看你啦!”小椿一邊大...
    YuShi_閱讀 1,242評論 10 9
  • 冬夜,一片枯葉,思念天空的微笑,明亮的借口,是對光陰要走的挽留。黑暗中,才開始關(guān)注,身體,早已失去水分的身體。 冬...
    宇斯閱讀 212評論 0 3
  • 在心理咨詢中,傾聽是必備技能,PET也如此。 前半年忙于工作忽視女兒,諸多行為的退行,連摔跤也會大鬧的情緒經(jīng)常發(fā)生...
    文刀祐閱讀 486評論 0 1
  • 2017年7月21日是咱們組第三次作業(yè),我因為在孩子學(xué)校參加一個準(zhǔn)備會,所以沒能參與大范圍的點評戰(zhàn)友作業(yè),小組的作...
    一汪青水閱讀 244評論 2 5

友情鏈接更多精彩內(nèi)容