返回顶部

274 & 275

今早又没打比赛

忘得干干净净

先反省一下

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数

根据维基百科上 h 指数的定义h 代表“高引用次数” ,一名科研人员的 h 指数 是指他(她)至少发表了 h 篇论文,并且每篇论文 至少 被引用 h 次。如果 h 有多种可能的值,h 指数 是其中最大的那个。

 

示例 1:

输入:citations = [3,0,6,1,5]
输出:3 
解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
     由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3

class Solution(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        ans=list()
        citations=sorted(citations)
        n = len(citations)
        for i in range(n):
            for j in range(citations[i]+1):
                if n-i>=j :
                    ans.append(j)
        return max(ans)

第一个通过的版本

然后觉得第二个循环 如果倒着来 可能会优化一点

class Solution(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        ans=list()
        citations=sorted(citations)
        n = len(citations)
        for i in range(n):
            for j in range(citations[i],-1,-1):
                if n-i>=j :
                    ans.append(j)
                    break
        return max(ans)

 

 

---------------------------------------------------------------------------------------------2023.10.30更新-------275---------------------------------------------------------------------------------------------------------

给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数,citations 已经按照 升序排列 。计算并返回该研究者的 h 指数。

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

请你设计并实现对数时间复杂度的算法解决此问题。

class Solution(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        ans=list()
        n = len(citations)
        for i in range(n):
            for j in range(citations[i],-1,-1):
                if n-i>=j :
                    ans.append(j)
                    break
        return max(ans)

 没怎么修改 感觉时间复杂度的要求应该没达到 

posted @ 2023-10-29 14:10  YuhangLiuCE  阅读(4)  评论(0编辑  收藏  举报