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

文章目录

一、题目

Given an array of integers citations where citationsi 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 <= citationsi <= 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 分钟前
LeetCode 350. 两个数组的交集 II|Python 解法详解
python·算法·leetcode
raindayinrain7 分钟前
c++并发
c++
j7~10 分钟前
【C++】智能指针的使用及其原理--详解
c++·c++11·内存泄漏·智能指针·raii·boost智能指针
阳明山水43 分钟前
销量预测2025—2026:从模型竞赛到系统融合的范式转型
人工智能·深度学习·算法·机器学习·架构
桐盛科技1 小时前
拒绝“漂绿”风险:基于边缘计算的碳排放数据清洗算法与实时校验逻辑
人工智能·算法·边缘计算
wuyk5551 小时前
107.FreeRTOS 链表深度解析:从原理到面试满分答案
c语言·开发语言·数据结构·stm32·单片机·链表·面试
-dzk-1 小时前
【链表】LC 138.随机链表的复制
数据结构·链表
Tongzhi20261 小时前
通芝科技考勤软件三大发展趋势:AI大模型、无感识别与数据底座
大数据·科技·算法·爬山算法
Persistent的粽子!2 小时前
C++:类与对象(一)
开发语言·c++·经验分享·笔记
eBest数字化转型方案2 小时前
Route Optimization for FMCG:多目标排线算法的工程实现
大数据·人工智能·算法