面试经典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;
    }
};
相关推荐
进击的圆儿10 小时前
10个TCP可靠性与拥塞控制题目整理
网络·c++·tcp/ip
墨染点香10 小时前
LeetCode 刷题【146. LRU 缓存】
leetcode·缓存·哈希算法
快手技术10 小时前
从“拦路虎”到“修路工”:基于AhaEdit的广告素材修复
前端·算法·架构
qk学算法10 小时前
力扣滑动窗口题目-76最小覆盖子串&&1234替换子串得到平衡字符串
数据结构·算法·leetcode
小欣加油10 小时前
leetcode 860 柠檬水找零
c++·算法·leetcode·职场和发展·贪心算法
还是码字踏实10 小时前
基础数据结构之数组的矩阵遍历:螺旋矩阵(LeetCode 54 中等题)
数据结构·leetcode·矩阵·螺旋矩阵
粉色挖掘机11 小时前
矩阵在密码学的应用——希尔密码详解
线性代数·算法·机器学习·密码学
买辣椒用券11 小时前
在Linux上实现Modbus RTU通信:一个轻量级C++解决方案
linux·c++
七七七七0711 小时前
【计算机网络】UDP协议深度解析:从报文结构到可靠性设计
服务器·网络·网络协议·计算机网络·算法·udp
TitosZhang11 小时前
排序算法稳定性判断
数据结构·算法·排序算法