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;
    }
};
相关推荐
智者知已应修善业4 分钟前
【输出一个N*N的01矩阵,表示最后的汉字点阵图】2024-10-22
c语言·数据结构·c++·经验分享·笔记·算法·矩阵
uesowys17 分钟前
华为OD算法开发指导-二级索引
数据结构·算法·华为od
苏宸啊19 分钟前
C++string(一)
开发语言·c++
a程序小傲30 分钟前
高并发下如何防止重复下单?
java·开发语言·算法·面试·职场和发展·状态模式
uoKent39 分钟前
c++中的封装、继承与多态
开发语言·c++·算法
爱喝可乐的老王40 分钟前
机器学习监督学习模型--朴素贝叶斯
人工智能·算法·机器学习
踏过山河,踏过海1 小时前
vs2019报错:Failed to connect to VCTIP: ‘CreateFile‘ failed with 2
c++
啊阿狸不会拉杆1 小时前
《机器学习》完结篇-总结
人工智能·算法·机器学习·计算机视觉·ai·集成学习·ml
Sheep Shaun1 小时前
C++11核心特性详解:从右值引用到现代C++编程
开发语言·数据结构·c++·算法
小王努力学编程1 小时前
LangChain——AI应用开发框架
服务器·c++·人工智能·分布式·rpc·langchain·brpc