面试经典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 小时前
2025年【江苏“信息与未来”编程思维】真题及题解(T2:坐标变换)
c++·题解·真题·坐标变换·编程思维·江苏·信息与未来
CS创新实验室2 小时前
算法、齿轮与硅基大脑:数值计算发展简史
人工智能·算法·数值计算
海石4 小时前
1563分的简单题,可能就简单在能被暴力AC
算法·leetcode
海石4 小时前
1400分的dp汗流浃背之【交替子数组计数】
算法·leetcode
奋发向前wcx4 小时前
P2590 树的统计 题目解析
数据结构·算法·深度优先
2023自学中5 小时前
C++ 内存追踪器
linux·c++
imbackneverdie5 小时前
AI4S不止于分子药物:以MedPeer为代表的科研基建打开产业新增量
大数据·人工智能·算法·aigc·科研·学术·ai 4s
额鹅恶饿呃6 小时前
C语言中的数据结构和变量
c语言·数据结构·算法
运行时记录7 小时前
prompt-optimizer skill
算法
万法若空8 小时前
【数据结构-哈希表】哈希表原理
数据结构·算法·散列表