給定一位研究者論文被引用次數的數組(被引用次數是非負整數)。編寫一個方法,計算出研究者的 h 指數。
h 指數的定義: “h 代表“高引用次數”(high citations),一名科研人員的 h 指數是指他(她)的 (N 篇論文中)至多有 h 篇論文分別被引用了至少 h 次。(其餘的 N - h 篇論文每篇被引用次數不多於 h 次。)”
示例:
輸入:citations = [3,0,6,1,5]
輸出: 3 解釋: 給定數組表示研究者總共有5
篇論文,每篇論文相應的被引用了3, 0, 6, 1, 5
次。 由於研究者有3
篇論文每篇至少被引用了3
次,其餘兩篇論文每篇被引用不多於3
次,所以她的 h 指數是3
。
說明: 如果 h 有多種可能的值,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