【LeetCode】275. H-Index II 解題報告(Python)
題目描述:
Given an array of citations sorted in ascending order
(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
Example:
Input: citations = [0,1,3,5,6] Output: 3 Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had received 0, 1, 3, 5, 6 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, her h-index is 3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
This is a follow up problem to H-Index, where citations
is now guaranteed to be sorted in ascending order.
Could you solve it in logarithmic time complexity?
題目大意
計算某個研究人員的影響因子。影響因子的計算方式是有h篇影響力至少為h的論文。影響因子是衡量作者生產力和影響力的方式,判斷了他又多少篇影響力很大的論文。和
解題方法
解釋一下樣例:[0,1,3,5,6],當h=0時表示至少有0篇影響力為0的論文;當h=1時表示至少有1篇影響力為1的論文;當h=3時表示至少有3篇影響力為3的論文;當h=5時表示至少有5篇影響力為5的論文;當h=6時表示至少有6篇影響力為6的論文.顯然符合要求的是隻要有3篇影響力為3的論文。
在274. H-Index中,是先排序再遍歷做的,這個題已經排好了序,所以比274更簡單,使用二分查詢可以快速求解。
我們求的結果就是求影響力x和不小於該影響力的論文個數的最小值,然後再求這個最小值的最大值。
時間複雜度是O(logN),空間複雜度是O(N)。
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
N = len(citations)
l, r = 0, N - 1
H = 0
while l <= r:
mid = l + (r - l) / 2
H = max(H, min(citations[mid], N - mid))
if citations[mid] < N - mid:
l = mid + 1
else:
r = mid - 1
return H
參考資料:
日期
2018 年 10 月 6 日 —— 努力看書