返回

[Leetcode]274. H-Index

题目描述

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

例子

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.

解题思路

首先要通过题目的不清楚描述用尽量简介的数学语言表示出来,由于考虑到各种大小比较;假设我们有 N 篇论文,并且已经排好序,我们要找出一个 Index,使其满足:

  • Index 篇论文的引用大于等于 Index: Index <= Citations[N - Index]
  • N - Index 篇论文的引用小于等于 Index: Citations[N - (Index + 1)] <= Index

根据这个思路可以写出第一个方法:

方法一

先排序,再从大到小遍历,根据上面的定义找出解,代码如下:

        if (citations.empty()) return 0;

        std::sort(citations.begin(), citations.end());
        int num_papers = citations.size();

        int pos = num_papers - 1;
        while (pos > 0) {
            int cite = citations[pos], last_cite = citations[pos - 1];
            int index = num_papers - pos;
            if (last_cite <= index && index <= cite) {
                return index;
            }
            pos--;
        }

        return min(num_papers, citations[0]);

时间复杂度:O(nlogn) 空间复杂度:O(1)

方法二

我们还可以用桶排序降低时间复杂度,桶排序的问题是需要桶的数量很多,造成空间复杂度很大,但是在这道题了 HIndex 最大也只能是所有论文的数量,所以我们可以把桶的数量降低到 n + 1 个,然后进行类似计算,代码如下:

class Solution {
public:
    int hIndex(vector<int>& citations) {

        int num_papers = citations.size();
        std::vector<int> papers(num_papers + 1, 0);
        for (int i = 0; i < citations.size(); i++) {
            citations[i] = min(num_papers, citations[i]);
            papers[citations[i]]++;
        }

        int count_papers = 0, index;
        for (index = num_papers; index >= 0; index--) {
            count_papers += papers[index];
            if (index <= count_papers) {
                break;
            }
        }

        return index;
    }
};

时间复杂度: O(n) 空间复杂度: O(n)

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy