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;
    }
};
相关推荐
郝学胜-神的一滴1 分钟前
Softmax 从入门到精通:多分类激活函数的优雅解法
人工智能·python·算法·机器学习·分类·数据挖掘
xianyinsuifeng3 分钟前
C语言性能优化实战:从 printf 到 write,再到批量输出(性能提升30%+)
算法
Halo_tjn4 分钟前
Java 内部类
java·开发语言·算法
开心码农1号7 分钟前
Go关于切边变量本身地址和内部指向地址
前端·算法
旖-旎7 分钟前
栈(验证栈序列)(5)
c++·算法·leetcode·力扣·
三毛的二哥8 分钟前
障碍物遮挡判断算法
人工智能·算法·计算机视觉·3d
啊我不会诶12 分钟前
2025ICPC南昌邀请赛vp补题
算法
码完就睡15 分钟前
数据结构——循环队列的设计及函数实现(C语言)
数据结构
发发就是发20 分钟前
I2C适配器与算法:从一次诡异的时序问题说起
服务器·驱动开发·单片机·嵌入式硬件·算法·fpga开发
啊哦呃咦唔鱼21 分钟前
leetcode二分查找
数据结构·算法·leetcode