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;
    }
};
相关推荐
前进之路91 天前
Leetcode每日一练--47
数据结构·算法·leetcode
2501_941236211 天前
C++与Node.js集成
开发语言·c++·算法
晨非辰1 天前
【数据结构初阶系列】归并排序全透视:从算法原理全分析到源码实战应用
运维·c语言·数据结构·c++·人工智能·python·深度学习
云边有个稻草人1 天前
部分移动(Partial Move)的使用场景:Rust 所有权拆分的精细化实践
开发语言·算法·rust
泡沫冰@1 天前
数据结构(20)
数据结构
松涛和鸣1 天前
11.C 语言学习:递归、宏定义、预处理、汉诺塔、Fibonacci 等
linux·c语言·开发语言·学习·算法·排序算法
2501_941111241 天前
C++与自动驾驶系统
开发语言·c++·算法
2501_941111691 天前
C++中的枚举类高级用法
开发语言·c++·算法
jz_ddk1 天前
[算法] 算法PK:LMS与RLS的对比研究
人工智能·神经网络·算法·信号处理·lms·rls·自适应滤波
Miraitowa_cheems1 天前
LeetCode算法日记 - Day 106: 两个字符串的最小ASCII删除和
java·数据结构·算法·leetcode·深度优先