面试经典150题——Day11

文章目录

一、题目

274. H-Index

Given an array of integers citations where citationsi 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 <= citationsi <= 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;
    }
};
相关推荐
局部有解1 分钟前
LeetCode Hot100 438 找到字符串中所有字母异位词
c++·算法·leetcode
垆边人似月.11 分钟前
移动长度 k 子串使字符串变成回文
数据结构·算法
David猪大卫11 分钟前
【C++修炼】string类:string类的模拟实现
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
pride.li21 分钟前
第12章 Python 教程-流程控制:循环结构
linux·服务器·python·算法
海绵宝宝转agent1 小时前
2026-10-16 4算法+1面经
算法
小poop1 小时前
基础排序算法详解——插入、选择与交换排序
算法·排序算法
青蓝智慧马老师1 小时前
想学数据分析但零基础,正确顺序不是先学 Python
大数据·人工智能·算法
jeo陽2 小时前
05-数组与冒泡排序:第一个数据结构与经典算法
c语言·数据结构·算法
WL学习笔记2 小时前
模板初阶(C++)
开发语言·c++·后端·模板初阶
鸽鸽程序猿2 小时前
【刷题册】hot21-25
算法