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;
    }
};
相关推荐
yugi9878387 分钟前
无线传感器网络中GAF算法节点特性分析
网络·算法
1027lonikitave37 分钟前
使用斐波那契数列讲解尾递归
算法
云深麋鹿1 小时前
标准库中的String类
开发语言·c++·容器
滴滴答滴答答2 小时前
LeetCode Hot100 之 16 合并两个有序链表
算法·leetcode·链表
ASKED_20192 小时前
企业级大模型微调(Fine-tuning)策略
大数据·人工智能·算法
圣保罗的大教堂2 小时前
leetcode 3713. 最长的平衡子串 I 中等
leetcode
t198751282 小时前
基于Chirp分解和多相快速算法的离散分数傅里叶变换(DFRFT)MATLAB实现
开发语言·算法·matlab
愚者游世3 小时前
力扣解决二进制 | 题型常用知识点梳理
c++·程序人生·算法·leetcode·职场和发展
阿星AI工作室3 小时前
宝藏skills!90个顶尖博客信源自动抓,AI每天帮我筛出20篇精华!
人工智能·算法
程序员酥皮蛋3 小时前
hot 100 第二十四题 24.回文链表
数据结构·链表