274. H-Index

Posted Premiumlab

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了274. H-Index相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/h-index/#/description

 

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 hcitations each, and the other N ? h papers have no more than h citations each."

For example, given citations = [3, 0, 6, 1, 5], which 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 3citations each and the remaining two with no more than 3 citations each, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.

 

Sol 1:

 

class Solution(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        # 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。 This means the index of element has something to do with the value of element/
        # Time O(nlogn) Space O(1)
        citations.sort()
        self.reverse(citations)
        for i in range(len(citations)):
            if i + 1  == citations[i]:
                return i + 1
            if i + 1 > citations[i]:
                return i
            
        return len(citations)
    
    
    def reverse(self, nums):
        left = 0
        right = len(nums) - 1
        while left < right:
            tmp = nums[left]
            nums[left] = nums[right]
            nums[right] = tmp
            left += 1
            right -=1
            
        return nums
        

 

 

 

Sol 2:

 

class Solution(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        # Time O(n) Space O(n)
        
        n = len(citations)
        citeCount = [0] * (n+1)
        for c in citations:
            if c >= n:
                citeCount[n] += 1
            else:
                citeCount[c] += 1
        
        sum = 0 # current number of papers
        for i in range(n, -1, -1):
            sum += citeCount[i]
            if sum >= i:
                return i
        
        return 0
        
        

 

以上是关于274. H-Index的主要内容,如果未能解决你的问题,请参考以下文章

274. H-Index

LeetCode 274. H-Index

java 274. H-Index(#)。java

java 274. H-Index(#)。java

java 274. H-Index(#)。java

java 274. H-Index(#)。java