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;
    }
};
相关推荐
yuanpan6 分钟前
Python 调用 DLL 动态库入门:Windows 下调用 C++ 与 C# 动态库完整示例
c++·windows·python
Yzzz-F11 分钟前
Problem - D - Codeforces
算法
chas_8815 分钟前
macbook air M5 32G本地跑ddtree-mlx效果
算法
programhelp_17 分钟前
WeRide OA 2026 高频真题分享 & 详细备战指南
经验分享·算法·面试·职场和发展
疯狂打码的少年26 分钟前
单向循环链表 + 尾指针:让插入删除更高效的秘密武器
数据结构·python·链表
菜菜的顾清寒42 分钟前
Leetcode (18) 力扣100 矩阵置零
算法
董董灿是个攻城狮1 小时前
5分钟搞懂微调的能力退化问题
算法
handler011 小时前
Linux: 基本指令知识点(3)
linux·服务器·c语言·开发语言·c++·笔记
wuminyu1 小时前
专家视角看Java线程生命周期与上下文切换的本质
java·linux·c语言·jvm·c++
云深麋鹿1 小时前
C++ | 容器list
开发语言·c++·容器·list