LeetCode 275 H-Index II (Python)

Posted by 小明MaxMing on June 18, 2020

题目

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 h citations each.”

解题思路

对与第i篇文章,引用次数是c,那么就有n-i篇文章的引用次数>c,找到第一篇引用次数大于等于n-i,找的时候使用二分

代码

class Solution:
    def hIndex(self, citations: List[int]) -> int:
        n = len(citations)
        l, r = 0, n - 1
        while l <= r:
            mid = (l + r) // 2
            if citations[mid] == n - mid:
                return n - mid
            if citations[mid] > n - mid:
                r = mid - 1
            else:
                l = mid + 1
        return n - l

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅