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即可。