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;
    }
};
相关推荐
炽烈小老头3 分钟前
【每天学习一点算法 2025/12/25】爬楼梯
学习·算法·动态规划
睡醒了叭4 分钟前
图像分割-传统算法-阈值分割原理与实践
opencv·算法·计算机视觉
CoovallyAIHub15 分钟前
200亿美元“反向收购雇佣”?老黄天价应对谷歌TPU压力
深度学习·算法·计算机视觉
oioihoii16 分钟前
C++多线程中join与detach机制深度解析
java·jvm·c++
落尘29817 分钟前
Catlass 模板库调试调优经验与踩坑记录
算法
ytttr87317 分钟前
叠前同步反演纵波速度、横波速度和密度三参数
算法
初圣魔门首席弟子26 分钟前
智能指针使用bug
c++·算法
好易学·数据结构36 分钟前
可视化图解算法75:最长上升子序列(最长递增子序列)
数据结构·算法·leetcode·动态规划·力扣·牛客网
Jeremy爱编码36 分钟前
leetcode热题岛屿数量
算法·leetcode·职场和发展
闻缺陷则喜何志丹39 分钟前
【组合数学 动态规划】P6870 [COCI2019-2020#5] Zapina|普及+
c++·数学·算法·动态规划·组合数学