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;
    }
};
相关推荐
北顾笙980几秒前
day25-数据结构力扣
数据结构·算法·leetcode
lxh01134 分钟前
最接近的三数之和
java·数据结构·算法
天若有情6734 分钟前
原创C++设计模式:功能归一化——无继承、轻量版AOP,比传统OOP更优雅
开发语言·c++·设计模式·oop
小O的算法实验室6 分钟前
2026年SEVC,增强自适应大邻域搜索算法求解带有禁飞区及异构无人机的电动汽车路径规划问题,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
黎雁·泠崖20 分钟前
二叉树基础精讲(上):树形结构 · 二叉树概念 · 性质 · 遍历 · 基础操作全解析
java·数据结构·算法
Q741_14722 分钟前
每日一题 力扣 2515.到目标字符串的最短距离 循环数组 C++题解
c++·算法·leetcode
深邃-24 分钟前
【C语言】-自定义类型:结构体
c语言·开发语言·数据结构·c++·html5
Dfreedom.25 分钟前
聚类算法对比分析:K-Means、DBSCAN 与层次聚类
人工智能·算法·机器学习·kmeans·聚类
cmpxr_26 分钟前
【C】结构体的内存对齐
c语言·开发语言·算法
Olivia_0_0_32 分钟前
【面试题】C++面试题整理——具身智能 / 自动驾驶 / 嵌入式 / 后台开发通用
c++·面试