274. H 指数

https://leetcode-cn.com/problems/h-index/

解法一:排序

先排序,然后i从后往前遍历。h初始为0,若c[i] > h则h加一。 注意这里h是篇数。为何条件不是c[i] >= h?用反证法:因为若c[i] == h时h加一(有个细节:h不仅是篇数,还是引用次数的下限,即>=h)。此时第i篇不满足引用次数>=h

class Solution:
    def hIndex(self, citations: List[int]) -> int:
        citations.sort()
        h=0
        i = len(citations)-1
        while i >=0:
            if citations[i] > h:
                h += 1
            i -= 1
        return  h

最后更新于