面试经典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;
    }
};
相关推荐
玖釉-7 小时前
C++ 中的循环语句详解:while、do...while、for、嵌套循环与循环控制
开发语言·c++·算法
不做无法实现的梦~7 小时前
运动控制系统复习一览-----常考题目总结版本
算法
小短腿的代码世界7 小时前
信号路由风暴:Qt算法交易系统的高频信号分发架构
qt·算法·架构
阿文的代码库7 小时前
一文读懂GROUP BY 1,2 VS GROUP BY column_1, column_2 的区别
算法
欧米欧7 小时前
C++进阶数据结构之搜索二叉树
开发语言·数据结构·c++
青小莫7 小时前
C++之vector讲解
c++·stl
小江的记录本7 小时前
【Java基础】反射与注解:核心原理、自定义注解、注解解析方式(附《思维导图》+《面试高频考点清单》)
java·数据结构·python·mysql·spring·面试·maven
008爬虫实战录8 小时前
【码上爬】 题十:魔改算法 堆栈分析,找加密值过程详解
前端·python·算法
chao1898448 小时前
基于狮蚁群算法(ALO)的火电机组功能调度实现
人工智能·算法
Deep-w8 小时前
【MATLAB】含光伏 - 储能的家庭/工业微电网能量管理仿真研究
开发语言·算法·matlab