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;
    }
};
相关推荐
charlie1145141914 分钟前
深入理解CC++的编译与链接技术9:动态库细节
c语言·开发语言·c++·学习·动态库
isyoungboy10 分钟前
c++使用win新api替代DirectShow驱动uvc摄像头,可改c#驱动
开发语言·c++·c#
CoderYanger16 分钟前
贪心算法:3.最大数
java·算法·leetcode·贪心算法·1024程序员节
lxmyzzs16 分钟前
【图像算法 - 37】人机交互应用:基于 YOLOv12 与 OpenCV 的高精度人脸情绪检测系统实现
算法·yolo·人机交互·情绪识别
muyouking1116 分钟前
Zig 语言实战:实现高性能快速排序算法
算法·排序算法
世转神风-20 分钟前
qt-windows用户点击.exe,报错:缺少libgcc_s_seh-1.dll
c++·qt
CoderYanger22 分钟前
贪心算法:5.最长递增子序列
java·算法·leetcode·贪心算法·1024程序员节
慕容青峰24 分钟前
【牛客周赛 107】E 题【小苯的刷怪笼】题解
c++·算法·sublime text
算法熔炉29 分钟前
深度学习面试八股文(2)——训练
人工智能·深度学习·算法
EXtreme3537 分钟前
【数据结构】打破线性思维:树形结构与堆在C语言中的完美实现方案
c语言·数据结构·算法··heap·完全二叉树·topk