LeetCode每日一题——275. H-Index II

文章目录

一、题目

Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper and citations is sorted in ascending order, 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.

You must write an algorithm that runs in logarithmic time.

Example 1:

Input: citations = [0,1,3,5,6]

Output: 3

Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had received 0, 1, 3, 5, 6 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,2,100]

Output: 2

Constraints:

n == citations.length

1 <= n <= 105

0 <= citations[i] <= 1000

citations is sorted in ascending order.

二、题解

cpp 复制代码
class Solution {
public:
    int hIndex(vector<int>& citations) {
        int n = citations.size();
        int left = 0, right = n - 1;
        while(left <= right){
            int mid = (left + right) >> 1;
            int ret = n - mid;
            if(citations[mid] >= ret) right = mid - 1;
            else if(citations[mid] < ret) left = mid + 1;
        }
        return n - left;
    }
};
相关推荐
蓝海星梦11 小时前
GRPO 算法演进:2025 年 RL4LLM 领域 40+ 项改进工作全景解析
论文阅读·人工智能·深度学习·算法·自然语言处理·强化学习
拼好饭和她皆失11 小时前
图论:最小生成树,二分图详细模板及讲解
c++·算法·图论
傻小胖11 小时前
19.ETH-挖矿算法-北大肖臻老师客堂笔记
笔记·算法·区块链
郝学胜-神的一滴11 小时前
线性判别分析(LDA)原理详解与实战应用
人工智能·python·程序人生·算法·机器学习·数据挖掘·sklearn
阿猿收手吧!11 小时前
【C++】C++原子类型隐式转换解析
java·c++
HL_风神11 小时前
C++设计模式学习-工厂方法模式
c++·学习·设计模式
量子炒饭大师11 小时前
【C++入门】—— 【什么时候需要用到深拷贝】C++的类中何时需要用到深拷贝?保姆级别带你罗列所有可能!
java·c++·dubbo·深拷贝
明洞日记11 小时前
【软考每日一练026】软件工程深度解析:软件开发方法学的分类与应用实战
c++·ai·系统架构·软件工程·软考
ScilogyHunter11 小时前
CW方程的向量形式与解析形式
算法·矩阵·控制
蓝海星梦11 小时前
GRPO 算法演进——奖励设计篇
论文阅读·人工智能·深度学习·算法·自然语言处理·强化学习