274. H-Index

Description

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.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Solution

Sort and Binary search, time O(n * log n), space O(1)

先排序抛腕,然后找到第一個位置使得citations[i] >= n - i敞临,用二分查找即可型宝。

class Solution {
    public int hIndex(int[] citations) {
        if (citations == null || citations.length == 0) {
            return 0;
        }
        
        Arrays.sort(citations);
        int n = citations.length;
        int left = 0;
        int right = n - 1;
        
        while (left <= right) {
            int mid = left + (right - left) / 2;
            
            if (citations[mid] < n - mid) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        
        return n - left;
    }
}

Bucket sort, time O(n), space O(n)

下面這個做法還挺妙的潜叛,利用計數(shù)法去做。

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+1buckets, 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
class Solution {
    public int hIndex(int[] citations) {
        if (citations == null || citations.length == 0) {
            return 0;
        }
        
        int n = citations.length;
        int[] buckets = new int[n + 1];
        for (int citation : citations) {
            if (citation > n) {
                ++buckets[n];
            } else {
                ++buckets[citation];
            }
        }
        
        int count = 0;
        for (int i = n; i >= 0; --i) {
            count += buckets[i];
            if (count >= i) {
                return i;
            }
        }
        
        return 0;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坝冕,一起剝皮案震驚了整個濱河市徒探,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌喂窟,老刑警劉巖测暗,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件央串,死亡現(xiàn)場離奇詭異,居然都是意外死亡碗啄,警方通過查閱死者的電腦和手機质和,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來稚字,“玉大人饲宿,你說我怎么就攤上這事〉瑁” “怎么了瘫想?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長袄友。 經(jīng)常有香客問我殿托,道長,這世上最難降的妖魔是什么剧蚣? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任支竹,我火速辦了婚禮,結(jié)果婚禮上鸠按,老公的妹妹穿的比我還像新娘礼搁。我一直安慰自己,他們只是感情好目尖,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布馒吴。 她就那樣靜靜地躺著,像睡著了一般瑟曲。 火紅的嫁衣襯著肌膚如雪饮戳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天洞拨,我揣著相機與錄音扯罐,去河邊找鬼。 笑死烦衣,一個胖子當著我的面吹牛歹河,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播花吟,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼秸歧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了衅澈?” 一聲冷哼從身側(cè)響起键菱,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎今布,沒想到半個月后纱耻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芭梯,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年弄喘,在試婚紗的時候發(fā)現(xiàn)自己被綠了玖喘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡蘑志,死狀恐怖累奈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情急但,我是刑警寧澤澎媒,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站波桩,受9級特大地震影響戒努,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜镐躲,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一储玫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧萤皂,春花似錦撒穷、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至入录,卻和暖如春蛤奥,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背僚稿。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工喻括, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人贫奠。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像望蜡,于是被迫代替她去往敵國和親唤崭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,346評論 0 10
  • 朋友圈,是微信的一個偉大發(fā)明小泉,它讓你不需要主動聯(lián)系朋友就可以知道他們的近況芦疏。但是冕杠,現(xiàn)在的朋友圈越來越少看到一些朋友...
    北漂達叔閱讀 5,747評論 0 0
  • 今天我想說的是勤儉節(jié)約,勤儉節(jié)約是我們中華民族的傳統(tǒng)美德酸茴,無論我們在那里分预,無論我們是什么職業(yè),無論我們的收入高或低...
    Lzr_2017閱讀 264評論 0 6
  • 有一天薪捍,我去世了 恨我的愛我的 落下帷幕 第二天笼痹,我將埋在地下深處 恨我的和愛我的 從此孤獨 一年后,青草就是我的...
    李唐的小詩閱讀 208評論 3 3
  • 簡介 : 代碼 : 總結(jié) : 就相當于 :
    王一航閱讀 411評論 0 0