面试经典150题——Day11

文章目录

一、题目

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.

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

题目来源:leetcode

二、题解

cpp 复制代码
class Solution {
public:
    int hIndex(vector<int>& citations) {
        int n = citations.size();
        sort(citations.begin(),citations.end());
        int hIndex = 0;
        for(int i = 0;i < n;i++){
            //剩余几篇文章
            int paperNums = n - i;
            //若当前引用值已经大于等于剩余文章数
            if(citations[i] >= paperNums) return paperNums;
            if(paperNums >= citations[i] && citations[i] > hIndex){
                hIndex = citations[i];
            }
        }
        return hIndex;
    }
};
相关推荐
我是咸鱼不闲呀4 分钟前
力扣Hot100系列21(Java)——[多维动态规划]总结(不同路径,最小路径和,最长回文子串,最长公共子序列, 编辑距离)
java·leetcode·动态规划
lihao lihao7 分钟前
二分查找
java·数据结构·算法
Albert Edison7 分钟前
【C++11】可变参数模板
java·开发语言·c++
WolfGang0073219 分钟前
代码随想录算法训练营 Day15 | 二叉树 part05
数据结构·算法
sheeta199810 分钟前
LeetCode 每日一题笔记 2025.03.20 3567.子矩阵的最小绝对差
笔记·leetcode·矩阵
代码栈上的思考10 分钟前
消息队列持久化:文件存储设计与实现全解析
java·前端·算法
qq_4176950520 分钟前
内存对齐与缓存友好设计
开发语言·c++·算法
2301_8166512221 分钟前
实时系统下的C++编程
开发语言·c++·算法
晓纪同学21 分钟前
EffctiveC++_02第二章
java·jvm·c++
2401_8318249622 分钟前
C++与Python混合编程实战
开发语言·c++·算法