每日一题力扣274

给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。

h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。且其余的 N - h 篇论文每篇被引用次数 不超过 h 次。

例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/h-index
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

原来想的是暴力破解,for套for还有俩if

正解:

然后主要就是,排序后从小到大,当然排序还有nlogn复杂度

排序之后,从小到大对比,加入数组长度为5,最开始的数字,如果它大于5,那么h等于5,因为后面的数字肯定比最开始的数字大;但是第二个数字,它不能再和5比了,它要求降低了,毕竟剩下的数字只有4个了,即使它大于5,也无济于事;所以数字和谁比,要看剩下还有多少个数。

class Solution:
    def hIndex(self, citations: List[int]) -> int:
        citations.sort()
        n = len(citations)
        if n==0 or citations[-1]==0:
            return 0
        for i in range(n):
            if citations[i]>=n-i:
                return n-i 

其实也可以二分:

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

作者:youngfolk
链接:https://leetcode-cn.com/problems/h-index/solution/san-chong-jie-fa-zhu-bu-you-hua-by-youngfolk/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

posted @ 2021-02-25 00:41  小千北同学超爱写代码  阅读(64)  评论(0编辑  收藏  举报