跳至主要內容

274. H 指数


274. H 指数open in new window

🟠   🔖  数组 计数排序 排序  🔗 力扣open in new window LeetCodeopen in new window

题目

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 Wikipediaopen in new window: 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.

Example 1:

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, their h-index is 3.

Example 2:

Input: citations = [1,3,1]

Output: 1

Constraints:

  • n == citations.length
  • 1 <= n <= 5000
  • 0 <= citations[i] <= 1000

题目大意

给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数

根据维基百科上 h 指数的定义:h 代表“高引用次数” ,一名科研人员的 h 指数 是指他(她)至少发表了 h 篇论文,并且 至少h 篇论文被引用次数大于等于 h 。如果 h 有多种可能的值,h 指数 是其中最大的那个。

示例 1:

输入:citations = [3,0,6,1,5]

输出:3

解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。

由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。

示例 2:

输入:citations = [1,3,1]

输出:1

解题思路

  • 第一步:排序,将数组由大到小排序;
  • 第二步:从左到右遍历数组,如果 citations[i]>i 则说明至少有 i+1 篇论文被引用了至少 i+1 次;

复杂度分析

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

代码

/**
 * @param {number[]} citations
 * @return {number}
 */
var hIndex = function (citations) {
	citations.sort((a, b) => b - a);
	let res = 0;
	while (citations[res] > res) res++;
	return res;
};

相关题目

题号标题题解标签难度
275H 指数 IIopen in new window[✓]数组 二分查找