1. 程式人生 > >274. H-Index論文引用量

274. H-Index論文引用量

英文 n-n -i IE ssi tis return -- rom

[抄題]:

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, his h-index is 3.

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

[暴力解法]:

時間分析:

空間分析:

[優化後]:

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

[思維問題]:

以為n篇文章的引用量,有什麽相互關系:並沒有

[一句話思路]:

[輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況(不合法不合理的輸入):

[畫圖]:

技術分享圖片

[一刷]:

  1. 累加數組元素的時候最好用一個新的sum 變量,也不占空間。加到數組元素上容易出現範圍錯誤。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分鐘肉眼debug的結果]:

[總結]:

[復雜度]:Time complexity: O(就是n) Space complexity: O(n)

[英文數據結構或算法,為什麽不用別的數據結構或算法]:

這題比較特殊,數組元素 = 個數

計數排序(Counting sort)是一種穩定的線性時間排序算法。
計數排序使用一個額外的數組 {\displaystyle C}  C ,其中第i個元素是待排序數組 {\displaystyle A} A中值等於 {\displaystyle i} i的元素的個數。
然後根據數組 {\displaystyle C}  C 來將 {\displaystyle A} A中的元素排到正確的位置。

[算法思想:遞歸/分治/貪心]:

[關鍵模板化代碼]:

//for loop : add to bucket
        for (int c : citations) {
            if (c > n) bucket[n]++;
            else bucket[c]++;
        }

[其他解法]:

[Follow Up]:

排序後:

[LC給出的題目變變變]:

[代碼風格] :

技術分享圖片
class Solution {
    public int hIndex(int[] citations) {
        //cc
        if (citations == null || citations.length == 0) return 0;
        
        //ini: bucket[n + 1] form exception
        int n = citations.length;
        int[] bucket = new int[n + 1];
        
        //for loop : add to bucket
        for (int c : citations) {
            if (c > n) bucket[n]++;
            else bucket[c]++;
        }
        
        //count from back
        int count = 0;
        for (int i = n; i >= 0; i--) {
            count += bucket[i];
            if (count >= i) return i;
        }
        
        return 0;
    }
}
View Code

274. H-Index論文引用量