Leetcode 275. H-Index II

Problem

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.

Algorithm

Bineary search.

Code

python3 复制代码
class Solution:
    def hIndex(self, citations: List[int]) -> int:
        K = len(citations)
        if 1 == K:
            return (citations[0] >= 1) * 1
        
        L, R, = 0, len(citations) - 1
        while L < R:
            Mid = (L + R) // 2
            if citations[Mid] >= K - Mid:
                R = Mid
            else: L = Mid + 1

        while L <= R and citations[L] < K - L:
            L += 1
        return K - L
相关推荐
轮到我狗叫了10 分钟前
牛客.小红的子串牛客.kotori和抽卡牛客.循环汉诺塔牛客.ruby和薯条
java·开发语言·算法
高山有多高27 分钟前
详解文件操作
c语言·开发语言·数据库·c++·算法
乌萨奇也要立志学C++41 分钟前
【洛谷】队列相关经典算法题详解:模板队列、机器翻译、海港
算法
YuTaoShao1 小时前
【LeetCode 热题 100】49. 字母异位词分组
算法·leetcode·哈希算法
aliedudu2 小时前
决策树概念与原理
算法·决策树·机器学习
程序员Xu3 小时前
【LeetCode热题100道笔记】腐烂的橘子
笔记·算法·leetcode
阿维的博客日记3 小时前
LeetCode5最长回文子串
leetcode
天选之女wow4 小时前
【代码随想录算法训练营——Day4】链表——24.两两交换链表中的节点、19.删除链表的倒数第N个节点、面试题02.07.链表相交、142.环形链表II
数据结构·算法·leetcode·链表
THMAIL5 小时前
量化基金从小白到大师 - 金融数据获取大全:从免费API到Tick级数据实战指南
人工智能·python·深度学习·算法·机器学习·金融·kafka
纪元A梦5 小时前
贪心算法应用:数字孪生同步问题详解
java·算法·贪心算法