LeetCode 274 H-Index (Python)

Posted by 小明MaxMing on August 11, 2020

题目

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.”

解题思路

  1. 排序+二分,具体参见H-Index II
  2. 按引用次数进行基数排序,如果次数超过n就看做n,最后向前扫一遍,找到hindex

代码

class Solution:
    def hIndex(self, citations: List[int]) -> int:
        n = len(citations)
        paper = [0] * (n + 1)
        for c in citations:
            paper[min(n, c)] += 1
        s, h = paper[-1], n
        while h > s:
            h -= 1
            s += paper[h]
        return h

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅