Leetcode_Sort --274. H-Index [medium]

Given an array of citations (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.”
给定一个学者的文章引用数列表,写一个方程来计算学者的h-index
h-index定义为一个学者的文章引用数中,有h篇文章的引用数至少为h,剩下的引用数都是不多于h的

Example:

Input: citations = [3,0,6,1,5]
Output: 3 
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had 
             received 3, 0, 6, 1, 5 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.

Solution:

Python

class Solution:
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        h = 0
        citations.sort(reverse = True)
        for i in range(len(citations)):
            if citations[i]>h:
                h += 1
        return h  

解题思路:
本题其实很简单,这个题目抽象出来就是在数组中找到h个大于h的数(这里我觉得是有歧义的,英文中先说at least h citations each,也就是包含h,而后面又说 no more then ,从结果来判断,应该是h个大于h的数),剩下的数都不大于h。这里的h应该从0开始,我们先将数组进行降序排列,依次将数组中的数和h进行比较,当数组中的数大于h的时候,h就加一,然后再与数组的下一个数进行比较,直到数组遍历完成,返回h即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值