274. H-Index
题目大意
Given an array of integers citations where citations[i]
is the number of citations a researcher received for their ith paper, return the researcher’s h-index.
According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
中文释义
给定一个整数数组 citations
,其中 citations[i]
是研究人员发表的第 i
篇论文收到的引用次数,返回研究人员的 h 指数。
根据维基百科上 h 指数的定义:h 指数定义为满足以下条件的最大的 h:研究人员至少发表了 h 篇论文,每篇论文被引用了至少 h 次。
Example
Example 1:
- Input:
citations = [3,0,6,1,5]
- Output:
3
- Explanation:
[3,0,6,1,5]
表示研究人员总共发表了 5 篇论文,每篇分别收到了 3, 0, 6, 1, 5 次引用。
由于研究人员有 3 篇论文至少被引用了 3 次,其余两篇引用次数不超过 3 次,因此他们的 h 指数为 3。
Example 2:
- Input:
citations = [1,3,1]
- Output:
1
Constraints
n == citations.length
1 <= n <= 5000
0 <= citations[i] <= 1000
解题思路
算法描述
此代码旨在计算给定引用次数数组的 h 指数。算法的主要步骤如下:
-
排序引用次数数组:
- 使用
sort
函数对citations
数组进行降序排序。排序后,数组中较高的引用次数将排在前面。
- 使用
-
计算 h 指数:
- 初始化
h
为0。h
用于记录符合 h 指数定义的论文数量。 - 使用一个
while
循环,条件为h
小于数组长度且第h
个元素的引用次数大于h
。 - 在循环中,如果一个论文的引用次数大于当前的
h
值,h
增加1。这代表至少有h
篇论文的引用次数大于或等于h
。
- 初始化
-
返回结果:
- 循环结束时,
h
的值即为所求的 h 指数。
- 循环结束时,
代码示例
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end(), [](int a, int b){return a > b;});
int h = 0;
while (h < citations.size() && citations[h] > h) {
h++;
}
return h;
}
};