【LeetCode 面试经典150题】274. H-Index H指数

本文介绍如何通过编程解决计算研究人员H-指数的问题,利用排序和循环算法确定满足h论文被引用至少h次的论文数量。给定一个整数数组表示论文引用次数,返回研究人员的H-指数值。
摘要由CSDN通过智能技术生成

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 指数。算法的主要步骤如下:

  1. 排序引用次数数组:

    • 使用 sort 函数对 citations 数组进行降序排序。排序后,数组中较高的引用次数将排在前面。
  2. 计算 h 指数:

    • 初始化 h 为0。h 用于记录符合 h 指数定义的论文数量。
    • 使用一个 while 循环,条件为 h 小于数组长度且第 h 个元素的引用次数大于 h
    • 在循环中,如果一个论文的引用次数大于当前的 h 值,h 增加1。这代表至少有 h 篇论文的引用次数大于或等于 h
  3. 返回结果:

    • 循环结束时,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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值