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 leasth citations each, and the other N − h papers have no more than h citations each."
Example:
Input:citations = [3,0,6,1,5]
Output: 3 Explanation:[3,0,6,1,5]
means the researcher has5
papers in total and each of them had received3, 0, 6, 1, 5
citations respectively. Since the researcher has3
papers with at least3
citations each and the remaining two with no more than3
citations each, her h-index is3
.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
這道題讓我們求H指數,這個質數是用來衡量研究人員的學術水平的質數,定義為一個人的學術文章有n篇分別被引用了n次,那么H指數就是n。而且wiki上直接給出了算法,可以按照如下方法確定某人的H指數:1、將其發表的所有SCI論文按被引次數從高到低排序;2、從前往后查找排序后的列表,直到某篇論文的序號大於該論文被引次數。所得序號減一即為H指數。我也就沒多想,直接按照上面的方法寫出了代碼:
class Solution { public: int hIndex(vector<int>& citations) { sort(citations.begin(), citations.end(), greater<int>()); for (int i = 0; i < citations.size(); ++i) { if (i >= citations[i]) return i; } return citations.size(); } };
類似題目:
參考資料:
https://leetcode.com/problems/h-index/