274. H指數 難度 中等

給定一位研究者論文被引用次數的數組(被引用次數是非負整數)。編寫一個方法,計算出研究者的 指數。

h 指數的定義: “h 代表“高引用次數”(high citations),一名科研人員的 h 指數是指他(她)的 (N 篇論文中)至多有 h 篇論文分別被引用了至少 h 次。(其餘的 N - h 篇論文每篇被引用次數不多於 次。)”

示例:

輸入: citations = [3,0,6,1,5]
輸出: 3 
解釋: 給定數組表示研究者總共有 5 篇論文,每篇論文相應的被引用了 3, 0, 6, 1, 5 次。
     由於研究者有 3 篇論文每篇至少被引用了 3 次,其餘兩篇論文每篇被引用不多於 3 次,所以她的 h 指數是 3

說明: 如果 有多種可能的值,h 指數是其中最大的那個。

 

先排序,再二分法。

class Solution(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        self.h = 0
        citations.sort()
        length = len(citations) 
        start = -1
        end = length
        def test(start, end):
            if end-start <= 1:
                return
            i = (start+end)/2
            if length-i<=citations[i]:
                if length-i>self.h:
                    self.h = length-i
                end = i
            else:
                start = i
            test(start, end)
        test(start, end)
        return self.h

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章