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;
    }
};
相关推荐
不会写代码的ys7 分钟前
日志库封装(项目通用)
c++
rgeshfgreh7 分钟前
MPPI算法实战:运动规划新利器
算法
Xの哲學10 分钟前
Linux epoll 深度剖析: 从设计哲学到底层实现
linux·服务器·网络·算法·边缘计算
星火开发设计12 分钟前
C++ multiset 全面解析与实战指南
开发语言·数据结构·c++·学习·set·知识
小猪咪piggy21 分钟前
【leetcode100】回溯
数据结构·算法
一眼万里*e26 分钟前
MavLink消息协议
c++
m0_6038887130 分钟前
More Images, More Problems A Controlled Analysis of VLM Failure Modes
人工智能·算法·机器学习·ai·论文速览
恶魔泡泡糖37 分钟前
51单片机矩阵按键
c语言·算法·矩阵·51单片机
叶子20242238 分钟前
电力系统分析---对称分量法
算法
圣保罗的大教堂1 小时前
leetcode 3453. 分割正方形 I 中等
leetcode